7 bool movit_initialized = false;
8 float movit_texel_subpixel_precision;
9 bool movit_srgb_textures_supported;
11 // The rules for objects with nontrivial constructors in static scope
12 // are somewhat convoluted, and easy to mess up. We simply have a
13 // pointer instead (and never care to clean it up).
14 std::string *movit_data_directory = NULL;
18 void measure_texel_subpixel_precision()
20 static const unsigned width = 1024;
22 // Generate a destination texture to render to, and an FBO.
23 GLuint dst_texnum, fbo;
25 glGenTextures(1, &dst_texnum);
27 glBindTexture(GL_TEXTURE_2D, dst_texnum);
29 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
32 glGenFramebuffers(1, &fbo);
34 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
36 glFramebufferTexture2D(
44 // Now generate a simple texture that's just [0,1].
46 float texdata[] = { 0, 1 };
47 glGenTextures(1, &dst_texnum);
49 glBindTexture(GL_TEXTURE_1D, dst_texnum);
51 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
55 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
57 glEnable(GL_TEXTURE_1D);
63 glDisable(GL_DEPTH_TEST);
65 glDepthMask(GL_FALSE);
68 glViewport(0, 0, width, 1);
70 glMatrixMode(GL_PROJECTION);
72 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
74 glMatrixMode(GL_MODELVIEW);
78 // Draw the texture stretched over a long quad, interpolating it out.
79 // Note that since the texel center is in (0.5), we need to adjust the
80 // texture coordinates in order not to get long stretches of (1,1,1,...)
81 // at the start and (...,0,0,0) at the end.
85 glVertex2f(0.0f, 0.0f);
88 glVertex2f(1.0f, 0.0f);
91 glVertex2f(1.0f, 1.0f);
94 glVertex2f(0.0f, 1.0f);
99 glDisable(GL_TEXTURE_1D);
102 // Now read the data back and see what the card did.
103 // (We only look at the red channel; the others will surely be the same.)
104 // We assume a linear ramp; anything else will give sort of odd results here.
105 float out_data[width];
106 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
109 float biggest_jump = 0.0f;
110 for (unsigned i = 1; i < width; ++i) {
111 assert(out_data[i] >= out_data[i - 1]);
112 biggest_jump = std::max(biggest_jump, out_data[i] - out_data[i - 1]);
115 movit_texel_subpixel_precision = biggest_jump;
118 glBindTexture(GL_TEXTURE_1D, 0);
120 glBindFramebuffer(GL_FRAMEBUFFER, 0);
122 glDeleteFramebuffers(1, &fbo);
124 glDeleteTextures(1, &dst_texnum);
126 glDeleteTextures(1, &src_texnum);
130 void check_extensions()
132 // We fundamentally need FBOs and floating-point textures.
133 assert(glewIsSupported("GL_ARB_framebuffer_object") != 0);
134 assert(glewIsSupported("GL_ARB_texture_float") != 0);
136 // We assume that we can use non-power-of-two textures without restrictions.
137 assert(glewIsSupported("GL_ARB_texture_non_power_of_two") != 0);
139 // We also need GLSL fragment shaders.
140 assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
141 assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
143 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
144 // but no modern card would really not provide it.)
145 assert(glewIsSupported("GL_ARB_pixel_buffer_object") != 0);
147 // ResampleEffect uses RG textures to encode a two-component LUT.
148 assert(glewIsSupported("GL_ARB_texture_rg") != 0);
150 // sRGB texture decode would be nice, but are not mandatory
151 // (GammaExpansionEffect can do the same thing if needed).
152 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
157 void init_movit(const std::string& data_directory)
159 if (movit_initialized) {
163 movit_data_directory = new std::string(data_directory);
168 glPixelStorei(GL_PACK_ALIGNMENT, 1);
169 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
171 measure_texel_subpixel_precision();
174 movit_initialized = true;