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