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