14 bool movit_initialized = false;
15 MovitDebugLevel movit_debug_level = MOVIT_DEBUG_ON;
16 float movit_texel_subpixel_precision;
17 bool movit_srgb_textures_supported;
18 int movit_num_wrongly_rounded;
19 bool movit_shader_rounding_supported;
21 // The rules for objects with nontrivial constructors in static scope
22 // are somewhat convoluted, and easy to mess up. We simply have a
23 // pointer instead (and never care to clean it up).
24 string *movit_data_directory = NULL;
28 void measure_texel_subpixel_precision()
30 static const unsigned width = 4096;
32 // Generate a destination texture to render to, and an FBO.
33 GLuint dst_texnum, fbo;
35 glGenTextures(1, &dst_texnum);
37 glBindTexture(GL_TEXTURE_2D, dst_texnum);
39 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
42 glGenFramebuffers(1, &fbo);
44 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
46 glFramebufferTexture2D(
54 // Now generate a simple texture that's just [0,1].
56 float texdata[] = { 0, 1 };
57 glGenTextures(1, &src_texnum);
59 glBindTexture(GL_TEXTURE_1D, src_texnum);
61 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
63 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
65 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
67 glEnable(GL_TEXTURE_1D);
73 glDisable(GL_DEPTH_TEST);
75 glDepthMask(GL_FALSE);
78 glViewport(0, 0, width, 1);
80 glMatrixMode(GL_PROJECTION);
82 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
84 glMatrixMode(GL_MODELVIEW);
88 // Draw the texture stretched over a long quad, interpolating it out.
89 // Note that since the texel center is in (0.5), we need to adjust the
90 // texture coordinates in order not to get long stretches of (1,1,1,...)
91 // at the start and (...,0,0,0) at the end.
95 glVertex2f(0.0f, 0.0f);
98 glVertex2f(1.0f, 0.0f);
101 glVertex2f(1.0f, 1.0f);
104 glVertex2f(0.0f, 1.0f);
109 glDisable(GL_TEXTURE_1D);
112 // Now read the data back and see what the card did.
113 // (We only look at the red channel; the others will surely be the same.)
114 // We assume a linear ramp; anything else will give sort of odd results here.
115 float out_data[width];
116 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
119 float biggest_jump = 0.0f;
120 for (unsigned i = 1; i < width; ++i) {
121 assert(out_data[i] >= out_data[i - 1]);
122 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
125 movit_texel_subpixel_precision = biggest_jump;
128 glBindTexture(GL_TEXTURE_1D, 0);
130 glBindFramebuffer(GL_FRAMEBUFFER, 0);
132 glDeleteFramebuffers(1, &fbo);
134 glDeleteTextures(1, &dst_texnum);
136 glDeleteTextures(1, &src_texnum);
140 void measure_roundoff_problems()
142 // Generate a destination texture to render to, and an FBO.
143 GLuint dst_texnum, fbo;
145 glGenTextures(1, &dst_texnum);
147 glBindTexture(GL_TEXTURE_2D, dst_texnum);
149 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
152 glGenFramebuffers(1, &fbo);
154 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
156 glFramebufferTexture2D(
158 GL_COLOR_ATTACHMENT0,
164 // Now generate a texture where every value except the last should be
165 // rounded up to the next one. However, there are cards (in highly
166 // common use) that can't do this right, for unknown reasons.
169 for (int i = 0; i < 256; ++i) {
170 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
171 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
173 glGenTextures(1, &src_texnum);
175 glBindTexture(GL_TEXTURE_1D, src_texnum);
177 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
179 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
181 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, 512, 0, GL_LUMINANCE, GL_FLOAT, texdata);
183 glEnable(GL_TEXTURE_1D);
189 glDisable(GL_DEPTH_TEST);
191 glDepthMask(GL_FALSE);
194 glViewport(0, 0, 512, 1);
196 glMatrixMode(GL_PROJECTION);
198 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
200 glMatrixMode(GL_MODELVIEW);
204 // Draw the texture stretched over a long quad, interpolating it out.
208 glVertex2f(0.0f, 0.0f);
211 glVertex2f(1.0f, 0.0f);
214 glVertex2f(1.0f, 1.0f);
217 glVertex2f(0.0f, 1.0f);
222 glDisable(GL_TEXTURE_1D);
225 // Now read the data back and see what the card did. (Ignore the last value.)
226 // (We only look at the red channel; the others will surely be the same.)
227 unsigned char out_data[512];
228 glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
231 int wrongly_rounded = 0;
232 for (unsigned i = 0; i < 255; ++i) {
233 if (out_data[i * 2 + 0] != i) {
236 if (out_data[i * 2 + 1] != i + 1) {
241 movit_num_wrongly_rounded = wrongly_rounded;
244 glBindTexture(GL_TEXTURE_1D, 0);
246 glBindFramebuffer(GL_FRAMEBUFFER, 0);
248 glDeleteFramebuffers(1, &fbo);
250 glDeleteTextures(1, &dst_texnum);
252 glDeleteTextures(1, &src_texnum);
256 bool check_extensions()
258 // We fundamentally need FBOs and floating-point textures.
259 if (!glewIsSupported("GL_ARB_framebuffer_object")) return false;
260 if (!glewIsSupported("GL_ARB_texture_float")) return false;
262 // We assume that we can use non-power-of-two textures without restrictions.
263 if (!glewIsSupported("GL_ARB_texture_non_power_of_two")) return false;
265 // We also need GLSL fragment shaders.
266 if (!glewIsSupported("GL_ARB_fragment_shader")) return false;
267 if (!glewIsSupported("GL_ARB_shading_language_100")) return false;
269 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
270 // but no modern card would really not provide it.)
271 if (!glewIsSupported("GL_ARB_pixel_buffer_object")) return false;
273 // ResampleEffect uses RG textures to encode a two-component LUT.
274 if (!glewIsSupported("GL_ARB_texture_rg")) return false;
276 // sRGB texture decode would be nice, but are not mandatory
277 // (GammaExpansionEffect can do the same thing if needed).
278 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
280 // We may want to use round() at the end of the final shader,
281 // if supported. We need either GLSL 1.30 or this extension to do that,
282 // and 1.30 brings with it other things that we don't want to demand
284 movit_shader_rounding_supported = glewIsSupported("GL_EXT_gpu_shader4");
291 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
293 if (movit_initialized) {
297 movit_data_directory = new string(data_directory);
298 movit_debug_level = debug_level;
300 GLenum err = glewInit();
301 if (err != GLEW_OK) {
306 glPixelStorei(GL_PACK_ALIGNMENT, 1);
307 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
308 glDisable(GL_DITHER);
310 if (!check_extensions()) {
313 measure_texel_subpixel_precision();
314 measure_roundoff_problems();
316 movit_initialized = true;