]> git.sesse.net Git - movit/blob - flat_input.cpp
24b5709874ba9318aaa74b01508e51f6250d7bd0
[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_SHORT || 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_R) {
50                                 internal_format = GL_R32F;
51                         } else if (pixel_format == FORMAT_RG) {
52                                 internal_format = GL_RG32F;
53                         } else if (pixel_format == FORMAT_RGB) {
54                                 internal_format = GL_RGB32F;
55                         } else {
56                                 internal_format = GL_RGBA32F;
57                         }
58                 } else if (type == GL_HALF_FLOAT) {
59                         if (pixel_format == FORMAT_R) {
60                                 internal_format = GL_R16F;
61                         } else if (pixel_format == FORMAT_RG) {
62                                 internal_format = GL_RG16F;
63                         } else if (pixel_format == FORMAT_RGB) {
64                                 internal_format = GL_RGB16F;
65                         } else {
66                                 internal_format = GL_RGBA16F;
67                         }
68                 } else if (type == GL_UNSIGNED_SHORT) {
69                         if (pixel_format == FORMAT_R) {
70                                 internal_format = GL_R16;
71                         } else if (pixel_format == FORMAT_RG) {
72                                 internal_format = GL_RG16;
73                         } else if (pixel_format == FORMAT_RGB) {
74                                 internal_format = GL_RGB16;
75                         } else {
76                                 internal_format = GL_RGBA16;
77                         }
78                 } else if (output_linear_gamma) {
79                         assert(type == GL_UNSIGNED_BYTE);
80                         internal_format = GL_SRGB8_ALPHA8;
81                 } else {
82                         assert(type == GL_UNSIGNED_BYTE);
83                         if (pixel_format == FORMAT_R) {
84                                 internal_format = GL_R8;
85                         } else if (pixel_format == FORMAT_RG) {
86                                 internal_format = GL_RG8;
87                         } else if (pixel_format == FORMAT_RGB) {
88                                 internal_format = GL_RGB8;
89                         } else {
90                                 internal_format = GL_RGBA8;
91                         }
92                 }
93                 if (pixel_format == FORMAT_RGB) {
94                         format = GL_RGB;
95                 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
96                            pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
97                         format = GL_RGBA;
98                 } else if (pixel_format == FORMAT_BGR) {
99                         format = GL_BGR;
100                 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
101                            pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
102                         format = GL_BGRA;
103                 } else if (pixel_format == FORMAT_GRAYSCALE) {
104                         format = GL_LUMINANCE;
105                 } else if (pixel_format == FORMAT_RG) {
106                         format = GL_RG;
107                 } else {
108                         assert(false);
109                 }
110
111                 // (Re-)upload the texture.
112                 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
113                 glBindTexture(GL_TEXTURE_2D, texture_num);
114                 check_error();
115                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
116                 check_error();
117                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
118                 check_error();
119                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
120                 check_error();
121                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
122                 check_error();
123                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
124                 check_error();
125                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
126                 check_error();
127                 if (needs_mipmaps) {
128                         glGenerateMipmap(GL_TEXTURE_2D);
129                         check_error();
130                 }
131                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
132                 check_error();
133                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
134                 check_error();
135                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
136                 check_error();
137                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
138                 check_error();
139         } else {
140                 glBindTexture(GL_TEXTURE_2D, texture_num);
141                 check_error();
142         }
143
144         // Bind it to a sampler.
145         set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
146         ++*sampler_num;
147 }
148
149 string FlatInput::output_fragment_shader()
150 {
151         return read_file("flat_input.frag");
152 }
153
154 void FlatInput::invalidate_pixel_data()
155 {
156         if (texture_num != 0) {
157                 resource_pool->release_2d_texture(texture_num);
158                 texture_num = 0;
159         }
160 }
161
162 }  // namespace movit