10 bool movit_initialized = false;
11 MovitDebugLevel movit_debug_level = MOVIT_DEBUG_ON;
12 float movit_texel_subpixel_precision;
13 bool movit_srgb_textures_supported;
14 int movit_num_wrongly_rounded;
16 // The rules for objects with nontrivial constructors in static scope
17 // are somewhat convoluted, and easy to mess up. We simply have a
18 // pointer instead (and never care to clean it up).
19 std::string *movit_data_directory = NULL;
23 void measure_texel_subpixel_precision()
25 static const unsigned width = 4096;
27 // Generate a destination texture to render to, and an FBO.
28 GLuint dst_texnum, fbo;
30 glGenTextures(1, &dst_texnum);
32 glBindTexture(GL_TEXTURE_2D, dst_texnum);
34 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
37 glGenFramebuffers(1, &fbo);
39 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
41 glFramebufferTexture2D(
49 // Now generate a simple texture that's just [0,1].
51 float texdata[] = { 0, 1 };
52 glGenTextures(1, &src_texnum);
54 glBindTexture(GL_TEXTURE_1D, src_texnum);
56 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
58 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
60 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
62 glEnable(GL_TEXTURE_1D);
68 glDisable(GL_DEPTH_TEST);
70 glDepthMask(GL_FALSE);
73 glViewport(0, 0, width, 1);
75 glMatrixMode(GL_PROJECTION);
77 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
79 glMatrixMode(GL_MODELVIEW);
83 // Draw the texture stretched over a long quad, interpolating it out.
84 // Note that since the texel center is in (0.5), we need to adjust the
85 // texture coordinates in order not to get long stretches of (1,1,1,...)
86 // at the start and (...,0,0,0) at the end.
90 glVertex2f(0.0f, 0.0f);
93 glVertex2f(1.0f, 0.0f);
96 glVertex2f(1.0f, 1.0f);
99 glVertex2f(0.0f, 1.0f);
104 glDisable(GL_TEXTURE_1D);
107 // Now read the data back and see what the card did.
108 // (We only look at the red channel; the others will surely be the same.)
109 // We assume a linear ramp; anything else will give sort of odd results here.
110 float out_data[width];
111 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
114 float biggest_jump = 0.0f;
115 for (unsigned i = 1; i < width; ++i) {
116 assert(out_data[i] >= out_data[i - 1]);
117 biggest_jump = std::max(biggest_jump, out_data[i] - out_data[i - 1]);
120 movit_texel_subpixel_precision = biggest_jump;
123 glBindTexture(GL_TEXTURE_1D, 0);
125 glBindFramebuffer(GL_FRAMEBUFFER, 0);
127 glDeleteFramebuffers(1, &fbo);
129 glDeleteTextures(1, &dst_texnum);
131 glDeleteTextures(1, &src_texnum);
135 void measure_roundoff_problems()
137 // Generate a destination texture to render to, and an FBO.
138 GLuint dst_texnum, fbo;
140 glGenTextures(1, &dst_texnum);
142 glBindTexture(GL_TEXTURE_2D, dst_texnum);
144 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
147 glGenFramebuffers(1, &fbo);
149 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
151 glFramebufferTexture2D(
153 GL_COLOR_ATTACHMENT0,
159 // Now generate a texture where every value except the last should be
160 // rounded up to the next one. However, there are cards (in highly
161 // common use) that can't do this right, for unknown reasons.
164 for (int i = 0; i < 256; ++i) {
165 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
166 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
168 glGenTextures(1, &src_texnum);
170 glBindTexture(GL_TEXTURE_1D, src_texnum);
172 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
174 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
176 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, 512, 0, GL_LUMINANCE, GL_FLOAT, texdata);
178 glEnable(GL_TEXTURE_1D);
184 glDisable(GL_DEPTH_TEST);
186 glDepthMask(GL_FALSE);
189 glViewport(0, 0, 512, 1);
191 glMatrixMode(GL_PROJECTION);
193 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
195 glMatrixMode(GL_MODELVIEW);
199 // Draw the texture stretched over a long quad, interpolating it out.
203 glVertex2f(0.0f, 0.0f);
206 glVertex2f(1.0f, 0.0f);
209 glVertex2f(1.0f, 1.0f);
212 glVertex2f(0.0f, 1.0f);
217 glDisable(GL_TEXTURE_1D);
220 // Now read the data back and see what the card did. (Ignore the last value.)
221 // (We only look at the red channel; the others will surely be the same.)
222 unsigned char out_data[512];
223 glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
226 int wrongly_rounded = 0;
227 for (unsigned i = 0; i < 255; ++i) {
228 if (out_data[i * 2 + 0] != i) {
231 if (out_data[i * 2 + 1] != i + 1) {
236 movit_num_wrongly_rounded = wrongly_rounded;
239 glBindTexture(GL_TEXTURE_1D, 0);
241 glBindFramebuffer(GL_FRAMEBUFFER, 0);
243 glDeleteFramebuffers(1, &fbo);
245 glDeleteTextures(1, &dst_texnum);
247 glDeleteTextures(1, &src_texnum);
251 void check_extensions()
253 // We fundamentally need FBOs and floating-point textures.
254 assert(glewIsSupported("GL_ARB_framebuffer_object") != 0);
255 assert(glewIsSupported("GL_ARB_texture_float") != 0);
257 // We assume that we can use non-power-of-two textures without restrictions.
258 assert(glewIsSupported("GL_ARB_texture_non_power_of_two") != 0);
260 // We also need GLSL fragment shaders.
261 assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
262 assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
264 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
265 // but no modern card would really not provide it.)
266 assert(glewIsSupported("GL_ARB_pixel_buffer_object") != 0);
268 // ResampleEffect uses RG textures to encode a two-component LUT.
269 assert(glewIsSupported("GL_ARB_texture_rg") != 0);
271 // sRGB texture decode would be nice, but are not mandatory
272 // (GammaExpansionEffect can do the same thing if needed).
273 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
278 void init_movit(const std::string& data_directory, MovitDebugLevel debug_level)
280 if (movit_initialized) {
284 movit_data_directory = new std::string(data_directory);
285 movit_debug_level = debug_level;
290 glPixelStorei(GL_PACK_ALIGNMENT, 1);
291 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
292 glDisable(GL_DITHER);
294 measure_texel_subpixel_precision();
295 measure_roundoff_problems();
298 movit_initialized = true;