]> git.sesse.net Git - movit/blob - flat_input.h
Convert another glReadPixels() to RGBA.
[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                 return (movit_srgb_textures_supported &&
30                         type == GL_UNSIGNED_BYTE &&
31                         (image_format.gamma_curve == GAMMA_LINEAR ||
32                          image_format.gamma_curve == GAMMA_sRGB));
33         }
34         virtual AlphaHandling alpha_handling() const {
35                 switch (pixel_format) {
36                 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
37                         return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
38                 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
39                         return OUTPUT_POSTMULTIPLIED_ALPHA;
40                 case FORMAT_R:
41                 case FORMAT_RG:
42                 case FORMAT_RGB:
43                         return OUTPUT_BLANK_ALPHA;
44                 default:
45                         assert(false);
46                 }
47         }
48
49         std::string output_fragment_shader();
50
51         // Uploads the texture if it has changed since last time.
52         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
53
54         unsigned get_width() const { return width; }
55         unsigned get_height() const { return height; }
56         Colorspace get_color_space() const { return image_format.color_space; }
57         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
58         virtual bool is_single_texture() const { return true; }
59
60         // Tells the input where to fetch the actual pixel data. Note that if you change
61         // this data, you must either call set_pixel_data() again (using the same pointer
62         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
63         // on subsequent frames.
64         //
65         // The data can either be a regular pointer (if pbo==0), or a byte offset
66         // into a PBO. The latter will allow you to start uploading the texture data
67         // asynchronously to the GPU, if you have any CPU-intensive work between the
68         // call to set_pixel_data() and the actual rendering. In either case,
69         // the pointer (and PBO, if set) has to be valid at the time of the render call.
70         void set_pixel_data(const unsigned char *pixel_data, GLuint pbo = 0)
71         {
72                 assert(this->type == GL_UNSIGNED_BYTE);
73                 this->pixel_data = pixel_data;
74                 this->pbo = pbo;
75                 invalidate_pixel_data();
76         }
77
78         void set_pixel_data(const unsigned short *pixel_data, GLuint pbo = 0)
79         {
80                 assert(this->type == GL_UNSIGNED_SHORT);
81                 this->pixel_data = pixel_data;
82                 this->pbo = pbo;
83                 invalidate_pixel_data();
84         }
85
86         void set_pixel_data_fp16(const fp16_int_t *pixel_data, GLuint pbo = 0)
87         {
88                 assert(this->type == GL_HALF_FLOAT);
89                 this->pixel_data = pixel_data;
90                 this->pbo = pbo;
91                 invalidate_pixel_data();
92         }
93
94         void set_pixel_data(const float *pixel_data, GLuint pbo = 0)
95         {
96                 assert(this->type == GL_FLOAT);
97                 this->pixel_data = pixel_data;
98                 this->pbo = pbo;
99                 invalidate_pixel_data();
100         }
101
102         void invalidate_pixel_data();
103
104         void set_pitch(unsigned pitch) {
105                 this->pitch = pitch;
106                 invalidate_pixel_data();
107         }
108
109         virtual void inform_added(EffectChain *chain)
110         {
111                 resource_pool = chain->get_resource_pool();
112         }
113
114 private:
115         ImageFormat image_format;
116         MovitPixelFormat pixel_format;
117         GLenum type;
118         GLuint pbo, texture_num;
119         int output_linear_gamma, needs_mipmaps;
120         unsigned width, height, pitch;
121         const void *pixel_data;
122         ResourcePool *resource_pool;
123         bool fixup_swap_rb, fixup_red_to_grayscale;
124 };
125
126 }  // namespace movit
127
128 #endif // !defined(_MOVIT_FLAT_INPUT_H)