Split out some private utilities into effect_util.cpp, so we do not need to include...
[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 "util.h"
8
9 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
10         : image_format(image_format),
11           pixel_format(pixel_format),
12           type(type),
13           pbo(0),
14           texture_num(0),
15           needs_update(false),
16           finalized(false),
17           output_linear_gamma(false),
18           needs_mipmaps(false),
19           width(width),
20           height(height),
21           pitch(width),
22           pixel_data(NULL)
23 {
24         assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
25         register_int("output_linear_gamma", &output_linear_gamma);
26         register_int("needs_mipmaps", &needs_mipmaps);
27 }
28
29 FlatInput::~FlatInput()
30 {
31         if (pbo != 0) {
32                 glDeleteBuffers(1, &pbo);
33                 check_error();
34         }
35         if (texture_num != 0) {
36                 glDeleteTextures(1, &texture_num);
37                 check_error();
38         }
39 }
40
41 void FlatInput::finalize()
42 {
43         // Translate the input format to OpenGL's enums.
44         GLenum internal_format;
45         if (type == GL_FLOAT) {
46                 internal_format = GL_RGBA16F_ARB;
47         } else if (output_linear_gamma) {
48                 assert(type == GL_UNSIGNED_BYTE);
49                 internal_format = GL_SRGB8_ALPHA8;
50         } else {
51                 assert(type == GL_UNSIGNED_BYTE);
52                 internal_format = GL_RGBA8;
53         }
54         if (pixel_format == FORMAT_RGB) {
55                 format = GL_RGB;
56                 bytes_per_pixel = 3;
57         } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
58                    pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
59                 format = GL_RGBA;
60                 bytes_per_pixel = 4;
61         } else if (pixel_format == FORMAT_BGR) {
62                 format = GL_BGR;
63                 bytes_per_pixel = 3;
64         } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
65                    pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
66                 format = GL_BGRA;
67                 bytes_per_pixel = 4;
68         } else if (pixel_format == FORMAT_GRAYSCALE) {
69                 format = GL_LUMINANCE;
70                 bytes_per_pixel = 1;
71         } else {
72                 assert(false);
73         }
74         if (type == GL_FLOAT) {
75                 bytes_per_pixel *= sizeof(float);
76         }
77
78         // Create PBO to hold the texture holding the input image, and then the texture itself.
79         glGenBuffers(1, &pbo);
80         check_error();
81         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
82         check_error();
83         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
84         check_error();
85         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
86         check_error();
87         
88         glGenTextures(1, &texture_num);
89         check_error();
90         glBindTexture(GL_TEXTURE_2D, texture_num);
91         check_error();
92         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
93         check_error();
94         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
95         check_error();
96         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
97         check_error();
98         // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
99         // instead of calling glGenerateMipmap().
100         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps ? GL_TRUE : GL_FALSE);
101         check_error();
102         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
103         check_error();
104         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
105         check_error();
106
107         needs_update = true;
108         finalized = true;
109 }
110         
111 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
112 {
113         glActiveTexture(GL_TEXTURE0 + *sampler_num);
114         check_error();
115         glBindTexture(GL_TEXTURE_2D, texture_num);
116         check_error();
117
118         if (needs_update) {
119                 // Copy the pixel data into the PBO.
120                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
121                 check_error();
122                 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
123                 memcpy(mapped_pbo, pixel_data, pitch * height * bytes_per_pixel);
124                 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
125                 check_error();
126
127                 // Re-upload the texture from the PBO.
128                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
129                 check_error();
130                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, BUFFER_OFFSET(0));
131                 check_error();
132                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
133                 check_error();
134                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
135                 check_error();
136                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
137                 check_error();
138                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
139                 check_error();
140
141                 needs_update = false;
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 std::string FlatInput::output_fragment_shader()
150 {
151         return read_file("flat_input.frag");
152 }