]> git.sesse.net Git - movit/blob - flat_input.h
Round explicitly after dithering, for GPUs that don't do it properly themselves.
[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 "image_format.h"
10 #include "init.h"
11 #include "input.h"
12
13 // A FlatInput is the normal, “classic” case of an input, where everything
14 // comes from a single 2D array with chunky pixels.
15 class FlatInput : public Input {
16 public:
17         FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
18         ~FlatInput();
19
20         virtual std::string effect_type_id() const { return "FlatInput"; }
21
22         // Create the texture itself. We cannot do this in the constructor,
23         // because we don't necessarily know all the settings (sRGB texture,
24         // mipmap generation) at that point.
25         void finalize();
26
27         virtual bool can_output_linear_gamma() const {
28                 return (movit_srgb_textures_supported &&
29                         type == GL_UNSIGNED_BYTE &&
30                         (image_format.gamma_curve == GAMMA_LINEAR ||
31                          image_format.gamma_curve == GAMMA_sRGB));
32         }
33         virtual AlphaHandling alpha_handling() const {
34                 switch (pixel_format) {
35                 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
36                 case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
37                         return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
38                 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
39                 case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
40                         return OUTPUT_POSTMULTIPLIED_ALPHA;
41                 case FORMAT_RGB:
42                 case FORMAT_BGR:
43                 case FORMAT_GRAYSCALE:
44                         return OUTPUT_BLANK_ALPHA;
45                 default:
46                         assert(false);
47                 }
48         }
49
50         std::string output_fragment_shader();
51
52         // Uploads the texture if it has changed since last time.
53         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
54
55         unsigned get_width() const { return width; }
56         unsigned get_height() const { return height; }
57         Colorspace get_color_space() const { return image_format.color_space; }
58         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
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 float *pixel_data, GLuint pbo = 0)
79         {
80                 assert(this->type == GL_FLOAT);
81                 this->pixel_data = pixel_data;
82                 this->pbo = pbo;
83                 invalidate_pixel_data();
84         }
85
86         void invalidate_pixel_data()
87         {
88                 needs_update = true;
89         }
90
91         void set_pitch(unsigned pitch) {
92                 assert(!finalized);
93                 this->pitch = pitch;
94         }
95
96 private:
97         ImageFormat image_format;
98         MovitPixelFormat pixel_format;
99         GLenum format, type;
100         GLuint pbo, texture_num;
101         bool needs_update, finalized;
102         int output_linear_gamma, needs_mipmaps;
103         unsigned width, height, pitch;
104         const void *pixel_data;
105 };
106
107 #endif // !defined(_MOVIT_FLAT_INPUT_H)