]> git.sesse.net Git - movit/blob - init.cpp
Fix another issue where an input was used twice. Add unit tests, again.
[movit] / init.cpp
1 #include <GL/glew.h>
2 #include <string>
3
4 #include "init.h"
5 #include "util.h"
6
7 bool movit_initialized = false;
8 MovitDebugLevel movit_debug_level = MOVIT_DEBUG_ON;
9 float movit_texel_subpixel_precision;
10 bool movit_srgb_textures_supported;
11
12 // The rules for objects with nontrivial constructors in static scope
13 // are somewhat convoluted, and easy to mess up. We simply have a
14 // pointer instead (and never care to clean it up).
15 std::string *movit_data_directory = NULL;
16
17 namespace {
18
19 void measure_texel_subpixel_precision()
20 {
21         static const unsigned width = 1024;
22
23         // Generate a destination texture to render to, and an FBO.
24         GLuint dst_texnum, fbo;
25
26         glGenTextures(1, &dst_texnum);
27         check_error();
28         glBindTexture(GL_TEXTURE_2D, dst_texnum);
29         check_error();
30         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
31         check_error();
32
33         glGenFramebuffers(1, &fbo);
34         check_error();
35         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
36         check_error();
37         glFramebufferTexture2D(
38                 GL_FRAMEBUFFER,
39                 GL_COLOR_ATTACHMENT0,
40                 GL_TEXTURE_2D,
41                 dst_texnum,
42                 0);
43         check_error();
44
45         // Now generate a simple texture that's just [0,1].
46         GLuint src_texnum;
47         float texdata[] = { 0, 1 };
48         glGenTextures(1, &dst_texnum);
49         check_error();
50         glBindTexture(GL_TEXTURE_1D, dst_texnum);
51         check_error();
52         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53         check_error();
54         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
55         check_error();
56         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
57         check_error();
58         glEnable(GL_TEXTURE_1D);
59         check_error();
60
61         // Basic state.
62         glDisable(GL_BLEND);
63         check_error();
64         glDisable(GL_DEPTH_TEST);
65         check_error();
66         glDepthMask(GL_FALSE);
67         check_error();
68
69         glViewport(0, 0, width, 1);
70
71         glMatrixMode(GL_PROJECTION);
72         glLoadIdentity();
73         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
74
75         glMatrixMode(GL_MODELVIEW);
76         glLoadIdentity();
77         check_error();
78
79         // Draw the texture stretched over a long quad, interpolating it out.
80         // Note that since the texel center is in (0.5), we need to adjust the
81         // texture coordinates in order not to get long stretches of (1,1,1,...)
82         // at the start and (...,0,0,0) at the end.
83         glBegin(GL_QUADS);
84
85         glTexCoord1f(0.25f);
86         glVertex2f(0.0f, 0.0f);
87
88         glTexCoord1f(0.75f);
89         glVertex2f(1.0f, 0.0f);
90
91         glTexCoord1f(0.75f);
92         glVertex2f(1.0f, 1.0f);
93
94         glTexCoord1f(0.25f);
95         glVertex2f(0.0f, 1.0f);
96
97         glEnd();
98         check_error();
99
100         glDisable(GL_TEXTURE_1D);
101         check_error();
102
103         // Now read the data back and see what the card did.
104         // (We only look at the red channel; the others will surely be the same.)
105         // We assume a linear ramp; anything else will give sort of odd results here.
106         float out_data[width];
107         glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
108         check_error();
109
110         float biggest_jump = 0.0f;
111         for (unsigned i = 1; i < width; ++i) {
112                 assert(out_data[i] >= out_data[i - 1]);
113                 biggest_jump = std::max(biggest_jump, out_data[i] - out_data[i - 1]);
114         }
115
116         movit_texel_subpixel_precision = biggest_jump;
117
118         // Clean up.
119         glBindTexture(GL_TEXTURE_1D, 0);
120         check_error();
121         glBindFramebuffer(GL_FRAMEBUFFER, 0);
122         check_error();
123         glDeleteFramebuffers(1, &fbo);
124         check_error();
125         glDeleteTextures(1, &dst_texnum);
126         check_error();
127         glDeleteTextures(1, &src_texnum);
128         check_error();
129 }
130
131 void check_extensions()
132 {
133         // We fundamentally need FBOs and floating-point textures.
134         assert(glewIsSupported("GL_ARB_framebuffer_object") != 0);
135         assert(glewIsSupported("GL_ARB_texture_float") != 0);
136
137         // We assume that we can use non-power-of-two textures without restrictions.
138         assert(glewIsSupported("GL_ARB_texture_non_power_of_two") != 0);
139
140         // We also need GLSL fragment shaders.
141         assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
142         assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
143
144         // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
145         // but no modern card would really not provide it.)
146         assert(glewIsSupported("GL_ARB_pixel_buffer_object") != 0);
147
148         // ResampleEffect uses RG textures to encode a two-component LUT.
149         assert(glewIsSupported("GL_ARB_texture_rg") != 0);
150
151         // sRGB texture decode would be nice, but are not mandatory
152         // (GammaExpansionEffect can do the same thing if needed).
153         movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
154 }
155
156 }  // namespace
157
158 void init_movit(const std::string& data_directory, MovitDebugLevel debug_level)
159 {
160         if (movit_initialized) {
161                 return;
162         }
163
164         movit_data_directory = new std::string(data_directory);
165         movit_debug_level = debug_level;
166
167         glewInit();
168
169         // geez 
170         glPixelStorei(GL_PACK_ALIGNMENT, 1);
171         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
172
173         measure_texel_subpixel_precision();
174         check_extensions();
175
176         movit_initialized = true;
177 }