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