Merge branch 'epoxy'
[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                         if (pixel_format == FORMAT_RGB) {
106                                 internal_format = GL_SRGB8;
107                         } else if (pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
108                                 internal_format = GL_SRGB8_ALPHA8;
109                         } else {
110                                 assert(false);
111                         }
112                 } else {
113                         assert(type == GL_UNSIGNED_BYTE);
114                         if (pixel_format == FORMAT_R) {
115                                 internal_format = GL_R8;
116                         } else if (pixel_format == FORMAT_RG) {
117                                 internal_format = GL_RG8;
118                         } else if (pixel_format == FORMAT_RGB) {
119                                 internal_format = GL_RGB8;
120                         } else {
121                                 internal_format = GL_RGBA8;
122                         }
123                 }
124                 if (pixel_format == FORMAT_RGB) {
125                         format = GL_RGB;
126                 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
127                            pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
128                         format = GL_RGBA;
129                 } else if (pixel_format == FORMAT_RG) {
130                         format = GL_RG;
131                 } else if (pixel_format == FORMAT_R) {
132                         format = GL_RED;
133                 } else {
134                         assert(false);
135                 }
136
137                 // (Re-)upload the texture.
138                 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
139                 glBindTexture(GL_TEXTURE_2D, texture_num);
140                 check_error();
141                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
142                 check_error();
143                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
144                 check_error();
145                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
146                 check_error();
147                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
148                 check_error();
149                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
150                 check_error();
151                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
152                 check_error();
153                 if (needs_mipmaps) {
154                         glGenerateMipmap(GL_TEXTURE_2D);
155                         check_error();
156                 }
157                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
158                 check_error();
159                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
160                 check_error();
161                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
162                 check_error();
163                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
164                 check_error();
165         } else {
166                 glBindTexture(GL_TEXTURE_2D, texture_num);
167                 check_error();
168         }
169
170         // Bind it to a sampler.
171         set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
172         ++*sampler_num;
173 }
174
175 string FlatInput::output_fragment_shader()
176 {
177         char buf[256];
178         sprintf(buf, "#define FIXUP_SWAP_RB %d\n#define FIXUP_RED_TO_GRAYSCALE %d\n",
179                 fixup_swap_rb, fixup_red_to_grayscale);
180         return buf + read_file("flat_input.frag");
181 }
182
183 void FlatInput::invalidate_pixel_data()
184 {
185         if (texture_num != 0) {
186                 resource_pool->release_2d_texture(texture_num);
187                 texture_num = 0;
188         }
189 }
190
191 }  // namespace movit