12 bool movit_initialized = false;
13 MovitDebugLevel movit_debug_level = MOVIT_DEBUG_ON;
14 float movit_texel_subpixel_precision;
15 bool movit_srgb_textures_supported;
16 int movit_num_wrongly_rounded;
17 bool movit_shader_rounding_supported;
19 // The rules for objects with nontrivial constructors in static scope
20 // are somewhat convoluted, and easy to mess up. We simply have a
21 // pointer instead (and never care to clean it up).
22 string *movit_data_directory = NULL;
26 void measure_texel_subpixel_precision()
28 static const unsigned width = 4096;
30 // Generate a destination texture to render to, and an FBO.
31 GLuint dst_texnum, fbo;
33 glGenTextures(1, &dst_texnum);
35 glBindTexture(GL_TEXTURE_2D, dst_texnum);
37 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
40 glGenFramebuffers(1, &fbo);
42 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
44 glFramebufferTexture2D(
52 // Now generate a simple texture that's just [0,1].
54 float texdata[] = { 0, 1 };
55 glGenTextures(1, &src_texnum);
57 glBindTexture(GL_TEXTURE_1D, src_texnum);
59 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
61 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
63 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
65 glEnable(GL_TEXTURE_1D);
71 glDisable(GL_DEPTH_TEST);
73 glDepthMask(GL_FALSE);
76 glViewport(0, 0, width, 1);
78 glMatrixMode(GL_PROJECTION);
80 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
82 glMatrixMode(GL_MODELVIEW);
86 // Draw the texture stretched over a long quad, interpolating it out.
87 // Note that since the texel center is in (0.5), we need to adjust the
88 // texture coordinates in order not to get long stretches of (1,1,1,...)
89 // at the start and (...,0,0,0) at the end.
93 glVertex2f(0.0f, 0.0f);
96 glVertex2f(1.0f, 0.0f);
99 glVertex2f(1.0f, 1.0f);
102 glVertex2f(0.0f, 1.0f);
107 glDisable(GL_TEXTURE_1D);
110 // Now read the data back and see what the card did.
111 // (We only look at the red channel; the others will surely be the same.)
112 // We assume a linear ramp; anything else will give sort of odd results here.
113 float out_data[width];
114 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
117 float biggest_jump = 0.0f;
118 for (unsigned i = 1; i < width; ++i) {
119 assert(out_data[i] >= out_data[i - 1]);
120 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
123 movit_texel_subpixel_precision = biggest_jump;
126 glBindTexture(GL_TEXTURE_1D, 0);
128 glBindFramebuffer(GL_FRAMEBUFFER, 0);
130 glDeleteFramebuffers(1, &fbo);
132 glDeleteTextures(1, &dst_texnum);
134 glDeleteTextures(1, &src_texnum);
138 void measure_roundoff_problems()
140 // Generate a destination texture to render to, and an FBO.
141 GLuint dst_texnum, fbo;
143 glGenTextures(1, &dst_texnum);
145 glBindTexture(GL_TEXTURE_2D, dst_texnum);
147 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
150 glGenFramebuffers(1, &fbo);
152 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
154 glFramebufferTexture2D(
156 GL_COLOR_ATTACHMENT0,
162 // Now generate a texture where every value except the last should be
163 // rounded up to the next one. However, there are cards (in highly
164 // common use) that can't do this right, for unknown reasons.
167 for (int i = 0; i < 256; ++i) {
168 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
169 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
171 glGenTextures(1, &src_texnum);
173 glBindTexture(GL_TEXTURE_1D, src_texnum);
175 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
177 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
179 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, 512, 0, GL_LUMINANCE, GL_FLOAT, texdata);
181 glEnable(GL_TEXTURE_1D);
187 glDisable(GL_DEPTH_TEST);
189 glDepthMask(GL_FALSE);
192 glViewport(0, 0, 512, 1);
194 glMatrixMode(GL_PROJECTION);
196 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
198 glMatrixMode(GL_MODELVIEW);
202 // Draw the texture stretched over a long quad, interpolating it out.
206 glVertex2f(0.0f, 0.0f);
209 glVertex2f(1.0f, 0.0f);
212 glVertex2f(1.0f, 1.0f);
215 glVertex2f(0.0f, 1.0f);
220 glDisable(GL_TEXTURE_1D);
223 // Now read the data back and see what the card did. (Ignore the last value.)
224 // (We only look at the red channel; the others will surely be the same.)
225 unsigned char out_data[512];
226 glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
229 int wrongly_rounded = 0;
230 for (unsigned i = 0; i < 255; ++i) {
231 if (out_data[i * 2 + 0] != i) {
234 if (out_data[i * 2 + 1] != i + 1) {
239 movit_num_wrongly_rounded = wrongly_rounded;
242 glBindTexture(GL_TEXTURE_1D, 0);
244 glBindFramebuffer(GL_FRAMEBUFFER, 0);
246 glDeleteFramebuffers(1, &fbo);
248 glDeleteTextures(1, &dst_texnum);
250 glDeleteTextures(1, &src_texnum);
254 bool check_extensions()
256 // We fundamentally need FBOs and floating-point textures.
257 if (!glewIsSupported("GL_ARB_framebuffer_object")) return false;
258 if (!glewIsSupported("GL_ARB_texture_float")) return false;
260 // We assume that we can use non-power-of-two textures without restrictions.
261 if (!glewIsSupported("GL_ARB_texture_non_power_of_two")) return false;
263 // We also need GLSL fragment shaders.
264 if (!glewIsSupported("GL_ARB_fragment_shader")) return false;
265 if (!glewIsSupported("GL_ARB_shading_language_100")) return false;
267 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
268 // but no modern card would really not provide it.)
269 if (!glewIsSupported("GL_ARB_pixel_buffer_object")) return false;
271 // ResampleEffect uses RG textures to encode a two-component LUT.
272 if (!glewIsSupported("GL_ARB_texture_rg")) return false;
274 // sRGB texture decode would be nice, but are not mandatory
275 // (GammaExpansionEffect can do the same thing if needed).
276 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
278 // We may want to use round() at the end of the final shader,
279 // if supported. We need either GLSL 1.30 or this extension to do that,
280 // and 1.30 brings with it other things that we don't want to demand
282 movit_shader_rounding_supported = glewIsSupported("GL_EXT_gpu_shader4");
289 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
291 if (movit_initialized) {
295 movit_data_directory = new string(data_directory);
296 movit_debug_level = debug_level;
298 GLenum err = glewInit();
299 if (err != GLEW_OK) {
304 glPixelStorei(GL_PACK_ALIGNMENT, 1);
305 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
306 glDisable(GL_DITHER);
308 if (!check_extensions()) {
311 measure_texel_subpixel_precision();
312 measure_roundoff_problems();
314 movit_initialized = true;