]> git.sesse.net Git - movit/blob - flat_input.cpp
Add missing format LUMINANCE8.
[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 "resource_pool.h"
8 #include "util.h"
9
10 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
11         : image_format(image_format),
12           pixel_format(pixel_format),
13           type(type),
14           pbo(0),
15           texture_num(0),
16           needs_update(false),
17           finalized(false),
18           output_linear_gamma(false),
19           needs_mipmaps(false),
20           width(width),
21           height(height),
22           pitch(width),
23           pixel_data(NULL)
24 {
25         assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
26         register_int("output_linear_gamma", &output_linear_gamma);
27         register_int("needs_mipmaps", &needs_mipmaps);
28 }
29
30 FlatInput::~FlatInput()
31 {
32         if (texture_num != 0) {
33                 resource_pool->release_2d_texture(texture_num);
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         texture_num = resource_pool->create_2d_texture(internal_format, width, height);
68         glBindTexture(GL_TEXTURE_2D, texture_num);
69         check_error();
70         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
71         check_error();
72         glBindTexture(GL_TEXTURE_2D, 0);
73         check_error();
74
75         needs_update = true;
76         finalized = true;
77 }
78         
79 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
80 {
81         glActiveTexture(GL_TEXTURE0 + *sampler_num);
82         check_error();
83         glBindTexture(GL_TEXTURE_2D, texture_num);
84         check_error();
85         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
86         check_error();
87
88         if (needs_update) {
89                 // Re-upload the texture.
90                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
91                 check_error();
92                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
93                 check_error();
94                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
95                 check_error();
96                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
97                 check_error();
98                 if (needs_mipmaps) {
99                         glGenerateMipmap(GL_TEXTURE_2D);
100                         check_error();
101                 }
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 }