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