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