]> git.sesse.net Git - movit/blob - flat_input.cpp
c7f417b873ddc64496d07e5311fa24e1c91c1127
[movit] / flat_input.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <string.h>
4 #include <GL/gl.h>
5 #include <GL/glext.h>
6 #include <assert.h>
7
8 #include "flat_input.h"
9 #include "util.h"
10
11 FlatInput::FlatInput(ImageFormat image_format, unsigned width, unsigned height)
12         : image_format(image_format),
13           needs_update(false),
14           finalized(false),
15           output_linear_gamma(false),
16           needs_mipmaps(false),
17           width(width),
18           height(height),
19           pitch(width)
20 {
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 (output_linear_gamma) {
30                 internal_format = GL_SRGB8;
31         } else {
32                 internal_format = GL_RGBA8;
33         }
34         if (image_format.pixel_format == FORMAT_RGB) {
35                 format = GL_RGB;
36                 bytes_per_pixel = 3;
37         } else if (image_format.pixel_format == FORMAT_RGBA) {
38                 format = GL_RGBA;
39                 bytes_per_pixel = 4;
40         } else if (image_format.pixel_format == FORMAT_BGR) {
41                 format = GL_BGR;
42                 bytes_per_pixel = 3;
43         } else if (image_format.pixel_format == FORMAT_BGRA) {
44                 format = GL_BGRA;
45                 bytes_per_pixel = 4;
46         } else if (image_format.pixel_format == FORMAT_GRAYSCALE) {
47                 format = GL_LUMINANCE;
48                 bytes_per_pixel = 1;
49         } else {
50                 assert(false);
51         }
52
53         // Create PBO to hold the texture holding the input image, and then the texture itself.
54         glGenBuffers(1, &pbo);
55         check_error();
56         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
57         check_error();
58         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
59         check_error();
60         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
61         check_error();
62         
63         glGenTextures(1, &texture_num);
64         check_error();
65         glBindTexture(GL_TEXTURE_2D, texture_num);
66         check_error();
67         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
68         check_error();
69         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
70         check_error();
71         // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
72         // instead of calling glGenerateMipmap().
73         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps);
74         check_error();
75         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
76         check_error();
77         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
78         check_error();
79
80         needs_update = false;
81         finalized = true;
82 }
83         
84 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
85 {
86         glActiveTexture(GL_TEXTURE0 + *sampler_num);
87         check_error();
88         glBindTexture(GL_TEXTURE_2D, texture_num);
89         check_error();
90
91         if (needs_update) {
92                 // Copy the pixel data into the PBO.
93                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
94                 check_error();
95                 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
96                 memcpy(mapped_pbo, pixel_data, pitch * height * bytes_per_pixel);
97                 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
98                 check_error();
99
100                 // Re-upload the texture from the PBO.
101                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
102                 check_error();
103                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
104                 check_error();
105                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
106                 check_error();
107                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
108                 check_error();
109                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
110                 check_error();
111                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
112                 check_error();
113
114                 needs_update = false;
115         }
116
117         // Bind it to a sampler.
118         set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
119         ++*sampler_num;
120 }
121
122 std::string FlatInput::output_fragment_shader()
123 {
124         return read_file("flat_input.frag");
125 }