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;
18 // The rules for objects with nontrivial constructors in static scope
19 // are somewhat convoluted, and easy to mess up. We simply have a
20 // pointer instead (and never care to clean it up).
21 string *movit_data_directory = NULL;
25 void measure_texel_subpixel_precision()
27 static const unsigned width = 4096;
29 // Generate a destination texture to render to, and an FBO.
30 GLuint dst_texnum, fbo;
32 glGenTextures(1, &dst_texnum);
34 glBindTexture(GL_TEXTURE_2D, dst_texnum);
36 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
39 glGenFramebuffers(1, &fbo);
41 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
43 glFramebufferTexture2D(
51 // Now generate a simple texture that's just [0,1].
53 float texdata[] = { 0, 1 };
54 glGenTextures(1, &src_texnum);
56 glBindTexture(GL_TEXTURE_1D, src_texnum);
58 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
62 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
64 glEnable(GL_TEXTURE_1D);
70 glDisable(GL_DEPTH_TEST);
72 glDepthMask(GL_FALSE);
75 glViewport(0, 0, width, 1);
77 glMatrixMode(GL_PROJECTION);
79 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
81 glMatrixMode(GL_MODELVIEW);
85 // Draw the texture stretched over a long quad, interpolating it out.
86 // Note that since the texel center is in (0.5), we need to adjust the
87 // texture coordinates in order not to get long stretches of (1,1,1,...)
88 // at the start and (...,0,0,0) at the end.
92 glVertex2f(0.0f, 0.0f);
95 glVertex2f(1.0f, 0.0f);
98 glVertex2f(1.0f, 1.0f);
101 glVertex2f(0.0f, 1.0f);
106 glDisable(GL_TEXTURE_1D);
109 // Now read the data back and see what the card did.
110 // (We only look at the red channel; the others will surely be the same.)
111 // We assume a linear ramp; anything else will give sort of odd results here.
112 float out_data[width];
113 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
116 float biggest_jump = 0.0f;
117 for (unsigned i = 1; i < width; ++i) {
118 assert(out_data[i] >= out_data[i - 1]);
119 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
122 movit_texel_subpixel_precision = biggest_jump;
125 glBindTexture(GL_TEXTURE_1D, 0);
127 glBindFramebuffer(GL_FRAMEBUFFER, 0);
129 glDeleteFramebuffers(1, &fbo);
131 glDeleteTextures(1, &dst_texnum);
133 glDeleteTextures(1, &src_texnum);
137 void measure_roundoff_problems()
139 // Generate a destination texture to render to, and an FBO.
140 GLuint dst_texnum, fbo;
142 glGenTextures(1, &dst_texnum);
144 glBindTexture(GL_TEXTURE_2D, dst_texnum);
146 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
149 glGenFramebuffers(1, &fbo);
151 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
153 glFramebufferTexture2D(
155 GL_COLOR_ATTACHMENT0,
161 // Now generate a texture where every value except the last should be
162 // rounded up to the next one. However, there are cards (in highly
163 // common use) that can't do this right, for unknown reasons.
166 for (int i = 0; i < 256; ++i) {
167 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
168 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
170 glGenTextures(1, &src_texnum);
172 glBindTexture(GL_TEXTURE_1D, src_texnum);
174 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
178 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, 512, 0, GL_LUMINANCE, GL_FLOAT, texdata);
180 glEnable(GL_TEXTURE_1D);
186 glDisable(GL_DEPTH_TEST);
188 glDepthMask(GL_FALSE);
191 glViewport(0, 0, 512, 1);
193 glMatrixMode(GL_PROJECTION);
195 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
197 glMatrixMode(GL_MODELVIEW);
201 // Draw the texture stretched over a long quad, interpolating it out.
205 glVertex2f(0.0f, 0.0f);
208 glVertex2f(1.0f, 0.0f);
211 glVertex2f(1.0f, 1.0f);
214 glVertex2f(0.0f, 1.0f);
219 glDisable(GL_TEXTURE_1D);
222 // Now read the data back and see what the card did. (Ignore the last value.)
223 // (We only look at the red channel; the others will surely be the same.)
224 unsigned char out_data[512];
225 glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
228 int wrongly_rounded = 0;
229 for (unsigned i = 0; i < 255; ++i) {
230 if (out_data[i * 2 + 0] != i) {
233 if (out_data[i * 2 + 1] != i + 1) {
238 movit_num_wrongly_rounded = wrongly_rounded;
241 glBindTexture(GL_TEXTURE_1D, 0);
243 glBindFramebuffer(GL_FRAMEBUFFER, 0);
245 glDeleteFramebuffers(1, &fbo);
247 glDeleteTextures(1, &dst_texnum);
249 glDeleteTextures(1, &src_texnum);
253 void check_extensions()
255 // We fundamentally need FBOs and floating-point textures.
256 assert(glewIsSupported("GL_ARB_framebuffer_object") != 0);
257 assert(glewIsSupported("GL_ARB_texture_float") != 0);
259 // We assume that we can use non-power-of-two textures without restrictions.
260 assert(glewIsSupported("GL_ARB_texture_non_power_of_two") != 0);
262 // We also need GLSL fragment shaders.
263 assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
264 assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
266 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
267 // but no modern card would really not provide it.)
268 assert(glewIsSupported("GL_ARB_pixel_buffer_object") != 0);
270 // ResampleEffect uses RG textures to encode a two-component LUT.
271 assert(glewIsSupported("GL_ARB_texture_rg") != 0);
273 // sRGB texture decode would be nice, but are not mandatory
274 // (GammaExpansionEffect can do the same thing if needed).
275 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
280 void init_movit(const string& data_directory, MovitDebugLevel debug_level)
282 if (movit_initialized) {
286 movit_data_directory = new string(data_directory);
287 movit_debug_level = debug_level;
292 glPixelStorei(GL_PACK_ALIGNMENT, 1);
293 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
294 glDisable(GL_DITHER);
296 measure_texel_subpixel_precision();
297 measure_roundoff_problems();
300 movit_initialized = true;