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