]> git.sesse.net Git - movit/blob - flat_input.cpp
Retire the GL_GENERATE_MIPMAP hack for FlatInput.
[movit] / flat_input.cpp
1 #include <string.h>
2 #include <assert.h>
3 #include <GL/glew.h>
4
5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "util.h"
8
9 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
10         : image_format(image_format),
11           pixel_format(pixel_format),
12           type(type),
13           pbo(0),
14           texture_num(0),
15           needs_update(false),
16           finalized(false),
17           output_linear_gamma(false),
18           needs_mipmaps(false),
19           width(width),
20           height(height),
21           pitch(width),
22           pixel_data(NULL)
23 {
24         assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
25         register_int("output_linear_gamma", &output_linear_gamma);
26         register_int("needs_mipmaps", &needs_mipmaps);
27 }
28
29 FlatInput::~FlatInput()
30 {
31         if (texture_num != 0) {
32                 glDeleteTextures(1, &texture_num);
33                 check_error();
34         }
35 }
36
37 void FlatInput::finalize()
38 {
39         // Translate the input format to OpenGL's enums.
40         GLenum internal_format;
41         if (type == GL_FLOAT) {
42                 internal_format = GL_RGBA32F_ARB;
43         } else if (output_linear_gamma) {
44                 assert(type == GL_UNSIGNED_BYTE);
45                 internal_format = GL_SRGB8_ALPHA8;
46         } else {
47                 assert(type == GL_UNSIGNED_BYTE);
48                 internal_format = GL_RGBA8;
49         }
50         if (pixel_format == FORMAT_RGB) {
51                 format = GL_RGB;
52         } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
53                    pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
54                 format = GL_RGBA;
55         } else if (pixel_format == FORMAT_BGR) {
56                 format = GL_BGR;
57         } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
58                    pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
59                 format = GL_BGRA;
60         } else if (pixel_format == FORMAT_GRAYSCALE) {
61                 format = GL_LUMINANCE;
62         } else {
63                 assert(false);
64         }
65
66         // Create the texture itself.
67         glGenTextures(1, &texture_num);
68         check_error();
69         glBindTexture(GL_TEXTURE_2D, texture_num);
70         check_error();
71         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
72         check_error();
73         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
74         check_error();
75         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
76         check_error();
77         glBindTexture(GL_TEXTURE_2D, 0);
78         check_error();
79
80         needs_update = true;
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                 // Re-upload the texture.
93                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
94                 check_error();
95                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
96                 check_error();
97                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
98                 check_error();
99                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
100                 check_error();
101                 if (needs_mipmaps) {
102                         glGenerateMipmap(GL_TEXTURE_2D);
103                         check_error();
104                 }
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 }