]> git.sesse.net Git - movit/blob - flat_input.h
Unbreak demo build (it was broken due to a typo).
[movit] / flat_input.h
1 #ifndef _MOVIT_FLAT_INPUT_H
2 #define _MOVIT_FLAT_INPUT_H 1
3
4 #include <GL/glew.h>
5 #include <assert.h>
6 #include <string>
7
8 #include "effect.h"
9 #include "effect_chain.h"
10 #include "image_format.h"
11 #include "init.h"
12 #include "input.h"
13
14 class ResourcePool;
15
16 // A FlatInput is the normal, “classic” case of an input, where everything
17 // comes from a single 2D array with chunky pixels.
18 class FlatInput : public Input {
19 public:
20         FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
21         ~FlatInput();
22
23         virtual std::string effect_type_id() const { return "FlatInput"; }
24
25         // Create the texture itself. We cannot do this in the constructor,
26         // because we don't necessarily know all the settings (sRGB texture,
27         // mipmap generation) at that point.
28         void finalize();
29
30         virtual bool can_output_linear_gamma() const {
31                 return (movit_srgb_textures_supported &&
32                         type == GL_UNSIGNED_BYTE &&
33                         (image_format.gamma_curve == GAMMA_LINEAR ||
34                          image_format.gamma_curve == GAMMA_sRGB));
35         }
36         virtual AlphaHandling alpha_handling() const {
37                 switch (pixel_format) {
38                 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
39                 case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
40                         return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
41                 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
42                 case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
43                         return OUTPUT_POSTMULTIPLIED_ALPHA;
44                 case FORMAT_RGB:
45                 case FORMAT_BGR:
46                 case FORMAT_GRAYSCALE:
47                         return OUTPUT_BLANK_ALPHA;
48                 default:
49                         assert(false);
50                 }
51         }
52
53         std::string output_fragment_shader();
54
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);
57
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
63         // Tells the input where to fetch the actual pixel data. Note that if you change
64         // this data, you must either call set_pixel_data() again (using the same pointer
65         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
66         // on subsequent frames.
67         //
68         // The data can either be a regular pointer (if pbo==0), or a byte offset
69         // into a PBO. The latter will allow you to start uploading the texture data
70         // asynchronously to the GPU, if you have any CPU-intensive work between the
71         // call to set_pixel_data() and the actual rendering. In either case,
72         // the pointer (and PBO, if set) has to be valid at the time of the render call.
73         void set_pixel_data(const unsigned char *pixel_data, GLuint pbo = 0)
74         {
75                 assert(this->type == GL_UNSIGNED_BYTE);
76                 this->pixel_data = pixel_data;
77                 this->pbo = pbo;
78                 invalidate_pixel_data();
79         }
80
81         void set_pixel_data(const float *pixel_data, GLuint pbo = 0)
82         {
83                 assert(this->type == GL_FLOAT);
84                 this->pixel_data = pixel_data;
85                 this->pbo = pbo;
86                 invalidate_pixel_data();
87         }
88
89         void invalidate_pixel_data();
90
91         void set_pitch(unsigned pitch) {
92                 assert(!finalized);
93                 this->pitch = pitch;
94         }
95
96         virtual void inform_added(EffectChain *chain)
97         {
98                 resource_pool = chain->get_resource_pool();
99         }
100
101 private:
102         ImageFormat image_format;
103         MovitPixelFormat pixel_format;
104         GLenum internal_format, format, type;
105         GLuint pbo, texture_num;
106         bool finalized;
107         int output_linear_gamma, needs_mipmaps;
108         unsigned width, height, pitch;
109         const void *pixel_data;
110         ResourcePool *resource_pool;
111 };
112
113 #endif // !defined(_MOVIT_FLAT_INPUT_H)