]> git.sesse.net Git - movit/blob - flat_input.cpp
Emulate glReadPixels of GL_BLUE.
[movit] / flat_input.cpp
1 #include <string.h>
2 #include <assert.h>
3 #include <epoxy/gl.h>
4
5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "resource_pool.h"
8 #include "util.h"
9
10 using namespace std;
11
12 namespace movit {
13
14 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format_in, GLenum type, unsigned width, unsigned height)
15         : image_format(image_format),
16           type(type),
17           pbo(0),
18           texture_num(0),
19           output_linear_gamma(false),
20           needs_mipmaps(false),
21           width(width),
22           height(height),
23           pitch(width),
24           pixel_data(NULL),
25           fixup_swap_rb(false),
26           fixup_red_to_grayscale(false)
27 {
28         assert(type == GL_FLOAT || type == GL_HALF_FLOAT || type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_BYTE);
29         register_int("output_linear_gamma", &output_linear_gamma);
30         register_int("needs_mipmaps", &needs_mipmaps);
31
32         // Some types are not supported in all GL versions (e.g. GLES),
33         // and will corrected into the right format in the shader.
34         switch (pixel_format_in) {
35         case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
36                 pixel_format = FORMAT_RGBA_PREMULTIPLIED_ALPHA;
37                 fixup_swap_rb = true;
38                 break;
39         case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
40                 pixel_format = FORMAT_RGBA_POSTMULTIPLIED_ALPHA;
41                 fixup_swap_rb = true;
42                 break;
43         case FORMAT_BGR:
44                 pixel_format = FORMAT_RGB;
45                 fixup_swap_rb = true;
46                 break;
47         case FORMAT_GRAYSCALE:
48                 pixel_format = FORMAT_R;
49                 fixup_red_to_grayscale = true;
50                 break;
51         default:
52                 pixel_format = pixel_format_in;
53                 break;
54         }
55 }
56
57 FlatInput::~FlatInput()
58 {
59         if (texture_num != 0) {
60                 resource_pool->release_2d_texture(texture_num);
61         }
62 }
63
64 void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
65 {
66         glActiveTexture(GL_TEXTURE0 + *sampler_num);
67         check_error();
68
69         if (texture_num == 0) {
70                 // Translate the input format to OpenGL's enums.
71                 GLint internal_format;
72                 GLenum format;
73                 if (type == GL_FLOAT) {
74                         if (pixel_format == FORMAT_R) {
75                                 internal_format = GL_R32F;
76                         } else if (pixel_format == FORMAT_RG) {
77                                 internal_format = GL_RG32F;
78                         } else if (pixel_format == FORMAT_RGB) {
79                                 internal_format = GL_RGB32F;
80                         } else {
81                                 internal_format = GL_RGBA32F;
82                         }
83                 } else if (type == GL_HALF_FLOAT) {
84                         if (pixel_format == FORMAT_R) {
85                                 internal_format = GL_R16F;
86                         } else if (pixel_format == FORMAT_RG) {
87                                 internal_format = GL_RG16F;
88                         } else if (pixel_format == FORMAT_RGB) {
89                                 internal_format = GL_RGB16F;
90                         } else {
91                                 internal_format = GL_RGBA16F;
92                         }
93                 } else if (type == GL_UNSIGNED_SHORT) {
94                         if (pixel_format == FORMAT_R) {
95                                 internal_format = GL_R16;
96                         } else if (pixel_format == FORMAT_RG) {
97                                 internal_format = GL_RG16;
98                         } else if (pixel_format == FORMAT_RGB) {
99                                 internal_format = GL_RGB16;
100                         } else {
101                                 internal_format = GL_RGBA16;
102                         }
103                 } else if (output_linear_gamma) {
104                         assert(type == GL_UNSIGNED_BYTE);
105                         internal_format = GL_SRGB8_ALPHA8;
106                 } else {
107                         assert(type == GL_UNSIGNED_BYTE);
108                         if (pixel_format == FORMAT_R) {
109                                 internal_format = GL_R8;
110                         } else if (pixel_format == FORMAT_RG) {
111                                 internal_format = GL_RG8;
112                         } else if (pixel_format == FORMAT_RGB) {
113                                 internal_format = GL_RGB8;
114                         } else {
115                                 internal_format = GL_RGBA8;
116                         }
117                 }
118                 if (pixel_format == FORMAT_RGB) {
119                         format = GL_RGB;
120                 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
121                            pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
122                         format = GL_RGBA;
123                 } else if (pixel_format == FORMAT_RG) {
124                         format = GL_RG;
125                 } else if (pixel_format == FORMAT_R) {
126                         format = GL_RED;
127                 } else {
128                         assert(false);
129                 }
130
131                 // (Re-)upload the texture.
132                 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
133                 glBindTexture(GL_TEXTURE_2D, texture_num);
134                 check_error();
135                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
136                 check_error();
137                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
138                 check_error();
139                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
140                 check_error();
141                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
142                 check_error();
143                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
144                 check_error();
145                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
146                 check_error();
147                 if (needs_mipmaps) {
148                         glGenerateMipmap(GL_TEXTURE_2D);
149                         check_error();
150                 }
151                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
152                 check_error();
153                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
154                 check_error();
155                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
156                 check_error();
157                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
158                 check_error();
159         } else {
160                 glBindTexture(GL_TEXTURE_2D, texture_num);
161                 check_error();
162         }
163
164         // Bind it to a sampler.
165         set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
166         ++*sampler_num;
167 }
168
169 string FlatInput::output_fragment_shader()
170 {
171         char buf[256];
172         sprintf(buf, "#define FIXUP_SWAP_RB %d\n#define FIXUP_RED_TO_GRAYSCALE %d\n",
173                 fixup_swap_rb, fixup_red_to_grayscale);
174         return buf + read_file("flat_input.frag");
175 }
176
177 void FlatInput::invalidate_pixel_data()
178 {
179         if (texture_num != 0) {
180                 resource_pool->release_2d_texture(texture_num);
181                 texture_num = 0;
182         }
183 }
184
185 }  // namespace movit