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;
15 // The rules for objects with nontrivial constructors in static scope
16 // are somewhat convoluted, and easy to mess up. We simply have a
17 // pointer instead (and never care to clean it up).
18 std::string *movit_data_directory = NULL;
22 void measure_texel_subpixel_precision()
24 static const unsigned width = 4096;
26 // Generate a destination texture to render to, and an FBO.
27 GLuint dst_texnum, fbo;
29 glGenTextures(1, &dst_texnum);
31 glBindTexture(GL_TEXTURE_2D, dst_texnum);
33 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
36 glGenFramebuffers(1, &fbo);
38 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
40 glFramebufferTexture2D(
48 // Now generate a simple texture that's just [0,1].
50 float texdata[] = { 0, 1 };
51 glGenTextures(1, &dst_texnum);
53 glBindTexture(GL_TEXTURE_1D, dst_texnum);
55 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
57 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
59 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
61 glEnable(GL_TEXTURE_1D);
67 glDisable(GL_DEPTH_TEST);
69 glDepthMask(GL_FALSE);
72 glViewport(0, 0, width, 1);
74 glMatrixMode(GL_PROJECTION);
76 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
78 glMatrixMode(GL_MODELVIEW);
82 // Draw the texture stretched over a long quad, interpolating it out.
83 // Note that since the texel center is in (0.5), we need to adjust the
84 // texture coordinates in order not to get long stretches of (1,1,1,...)
85 // at the start and (...,0,0,0) at the end.
89 glVertex2f(0.0f, 0.0f);
92 glVertex2f(1.0f, 0.0f);
95 glVertex2f(1.0f, 1.0f);
98 glVertex2f(0.0f, 1.0f);
103 glDisable(GL_TEXTURE_1D);
106 // Now read the data back and see what the card did.
107 // (We only look at the red channel; the others will surely be the same.)
108 // We assume a linear ramp; anything else will give sort of odd results here.
109 float out_data[width];
110 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
113 float biggest_jump = 0.0f;
114 for (unsigned i = 1; i < width; ++i) {
115 assert(out_data[i] >= out_data[i - 1]);
116 biggest_jump = std::max(biggest_jump, out_data[i] - out_data[i - 1]);
119 movit_texel_subpixel_precision = biggest_jump;
122 glBindTexture(GL_TEXTURE_1D, 0);
124 glBindFramebuffer(GL_FRAMEBUFFER, 0);
126 glDeleteFramebuffers(1, &fbo);
128 glDeleteTextures(1, &dst_texnum);
130 glDeleteTextures(1, &src_texnum);
134 void check_extensions()
136 // We fundamentally need FBOs and floating-point textures.
137 assert(glewIsSupported("GL_ARB_framebuffer_object") != 0);
138 assert(glewIsSupported("GL_ARB_texture_float") != 0);
140 // We assume that we can use non-power-of-two textures without restrictions.
141 assert(glewIsSupported("GL_ARB_texture_non_power_of_two") != 0);
143 // We also need GLSL fragment shaders.
144 assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
145 assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
147 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
148 // but no modern card would really not provide it.)
149 assert(glewIsSupported("GL_ARB_pixel_buffer_object") != 0);
151 // ResampleEffect uses RG textures to encode a two-component LUT.
152 assert(glewIsSupported("GL_ARB_texture_rg") != 0);
154 // sRGB texture decode would be nice, but are not mandatory
155 // (GammaExpansionEffect can do the same thing if needed).
156 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
161 void init_movit(const std::string& data_directory, MovitDebugLevel debug_level)
163 if (movit_initialized) {
167 movit_data_directory = new std::string(data_directory);
168 movit_debug_level = debug_level;
173 glPixelStorei(GL_PACK_ALIGNMENT, 1);
174 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
176 measure_texel_subpixel_precision();
179 movit_initialized = true;