Merge branch 'master' into 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, GLenum type, unsigned width, unsigned height)
15         : image_format(image_format),
16           pixel_format(pixel_format),
17           type(type),
18           pbo(0),
19           texture_num(0),
20           output_linear_gamma(false),
21           needs_mipmaps(false),
22           width(width),
23           height(height),
24           pitch(width),
25           pixel_data(NULL)
26 {
27         assert(type == GL_FLOAT || type == GL_HALF_FLOAT || type == GL_UNSIGNED_BYTE);
28         register_int("output_linear_gamma", &output_linear_gamma);
29         register_int("needs_mipmaps", &needs_mipmaps);
30 }
31
32 FlatInput::~FlatInput()
33 {
34         if (texture_num != 0) {
35                 resource_pool->release_2d_texture(texture_num);
36         }
37 }
38
39 void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
40 {
41         glActiveTexture(GL_TEXTURE0 + *sampler_num);
42         check_error();
43
44         if (texture_num == 0) {
45                 // Translate the input format to OpenGL's enums.
46                 GLint internal_format;
47                 GLenum format;
48                 if (type == GL_FLOAT) {
49                         if (pixel_format == FORMAT_RG) {
50                                 internal_format = GL_RG32F;
51                         } else {
52                                 internal_format = GL_RGBA32F;
53                         }
54                 } else if (type == GL_HALF_FLOAT) {
55                         if (pixel_format == FORMAT_RG) {
56                                 internal_format = GL_RG16F;
57                         } else {
58                                 internal_format = GL_RGBA16F;
59                         }
60                 } else if (output_linear_gamma) {
61                         assert(type == GL_UNSIGNED_BYTE);
62                         internal_format = GL_SRGB8_ALPHA8;
63                 } else {
64                         assert(type == GL_UNSIGNED_BYTE);
65                         internal_format = GL_RGBA8;
66                 }
67                 if (pixel_format == FORMAT_RGB) {
68                         format = GL_RGB;
69                 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
70                            pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
71                         format = GL_RGBA;
72                 } else if (pixel_format == FORMAT_BGR) {
73                         format = GL_BGR;
74                 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
75                            pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
76                         format = GL_BGRA;
77                 } else if (pixel_format == FORMAT_GRAYSCALE) {
78                         format = GL_LUMINANCE;
79                 } else if (pixel_format == FORMAT_RG) {
80                         format = GL_RG;
81                 } else {
82                         assert(false);
83                 }
84
85                 // (Re-)upload the texture.
86                 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
87                 glBindTexture(GL_TEXTURE_2D, texture_num);
88                 check_error();
89                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
90                 check_error();
91                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
92                 check_error();
93                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94                 check_error();
95                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
96                 check_error();
97                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
98                 check_error();
99                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
100                 check_error();
101                 if (needs_mipmaps) {
102                         glGenerateMipmap(GL_TEXTURE_2D);
103                         check_error();
104                 }
105                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
106                 check_error();
107                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
108                 check_error();
109                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
110                 check_error();
111                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
112                 check_error();
113         } else {
114                 glBindTexture(GL_TEXTURE_2D, texture_num);
115                 check_error();
116         }
117
118         // Bind it to a sampler.
119         set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
120         ++*sampler_num;
121 }
122
123 string FlatInput::output_fragment_shader()
124 {
125         return read_file("flat_input.frag");
126 }
127
128 void FlatInput::invalidate_pixel_data()
129 {
130         if (texture_num != 0) {
131                 resource_pool->release_2d_texture(texture_num);
132                 texture_num = 0;
133         }
134 }
135
136 }  // namespace movit