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