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;
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;
19 void measure_texel_subpixel_precision()
21 static const unsigned width = 1024;
23 // Generate a destination texture to render to, and an FBO.
24 GLuint dst_texnum, fbo;
26 glGenTextures(1, &dst_texnum);
28 glBindTexture(GL_TEXTURE_2D, dst_texnum);
30 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
33 glGenFramebuffers(1, &fbo);
35 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
37 glFramebufferTexture2D(
45 // Now generate a simple texture that's just [0,1].
47 float texdata[] = { 0, 1 };
48 glGenTextures(1, &dst_texnum);
50 glBindTexture(GL_TEXTURE_1D, dst_texnum);
52 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
56 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
58 glEnable(GL_TEXTURE_1D);
64 glDisable(GL_DEPTH_TEST);
66 glDepthMask(GL_FALSE);
69 glViewport(0, 0, width, 1);
71 glMatrixMode(GL_PROJECTION);
73 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
75 glMatrixMode(GL_MODELVIEW);
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.
86 glVertex2f(0.0f, 0.0f);
89 glVertex2f(1.0f, 0.0f);
92 glVertex2f(1.0f, 1.0f);
95 glVertex2f(0.0f, 1.0f);
100 glDisable(GL_TEXTURE_1D);
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);
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]);
116 movit_texel_subpixel_precision = biggest_jump;
119 glBindTexture(GL_TEXTURE_1D, 0);
121 glBindFramebuffer(GL_FRAMEBUFFER, 0);
123 glDeleteFramebuffers(1, &fbo);
125 glDeleteTextures(1, &dst_texnum);
127 glDeleteTextures(1, &src_texnum);
131 void check_extensions()
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);
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);
140 // We also need GLSL fragment shaders.
141 assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
142 assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
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);
148 // ResampleEffect uses RG textures to encode a two-component LUT.
149 assert(glewIsSupported("GL_ARB_texture_rg") != 0);
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");
158 void init_movit(const std::string& data_directory, MovitDebugLevel debug_level)
160 if (movit_initialized) {
164 movit_data_directory = new std::string(data_directory);
165 movit_debug_level = debug_level;
170 glPixelStorei(GL_PACK_ALIGNMENT, 1);
171 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
173 measure_texel_subpixel_precision();
176 movit_initialized = true;