]> git.sesse.net Git - movit/blob - flat_input.cpp
There's no need to #undef PREFIX, since we do the token pasting ourselves.
[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           owns_texture(false),
25           pixel_data(NULL),
26           fixup_swap_rb(false),
27           fixup_red_to_grayscale(false)
28 {
29         assert(type == GL_FLOAT || type == GL_HALF_FLOAT || type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_BYTE);
30         register_int("output_linear_gamma", &output_linear_gamma);
31         register_int("needs_mipmaps", &needs_mipmaps);
32         register_uniform_sampler2d("tex", &uniform_tex);
33
34         // Some types are not supported in all GL versions (e.g. GLES),
35         // and will corrected into the right format in the shader.
36         switch (pixel_format_in) {
37         case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
38                 pixel_format = FORMAT_RGBA_PREMULTIPLIED_ALPHA;
39                 fixup_swap_rb = true;
40                 break;
41         case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
42                 pixel_format = FORMAT_RGBA_POSTMULTIPLIED_ALPHA;
43                 fixup_swap_rb = true;
44                 break;
45         case FORMAT_BGR:
46                 pixel_format = FORMAT_RGB;
47                 fixup_swap_rb = true;
48                 break;
49         case FORMAT_GRAYSCALE:
50                 pixel_format = FORMAT_R;
51                 fixup_red_to_grayscale = true;
52                 break;
53         default:
54                 pixel_format = pixel_format_in;
55                 break;
56         }
57 }
58
59 FlatInput::~FlatInput()
60 {
61         possibly_release_texture();
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 && (pbo != 0 || pixel_data != NULL)) {
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                 owns_texture = true;
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         possibly_release_texture();
187 }
188
189 void FlatInput::possibly_release_texture()
190 {
191         if (texture_num != 0 && owns_texture) {
192                 resource_pool->release_2d_texture(texture_num);
193                 texture_num = 0;
194                 owns_texture = false;
195         }
196 }
197
198 }  // namespace movit