8 bool movit_initialized = false;
9 float movit_texel_subpixel_precision;
10 bool movit_srgb_textures_supported;
14 void measure_texel_subpixel_precision()
16 static const unsigned width = 1024;
18 // Generate a destination texture to render to, and an FBO.
19 GLuint dst_texnum, fbo;
21 glGenTextures(1, &dst_texnum);
23 glBindTexture(GL_TEXTURE_2D, dst_texnum);
25 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
28 glGenFramebuffers(1, &fbo);
30 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
32 glFramebufferTexture2D(
40 // Now generate a simple texture that's just [0,1].
42 float texdata[] = { 0, 1 };
43 glGenTextures(1, &dst_texnum);
45 glBindTexture(GL_TEXTURE_1D, dst_texnum);
47 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
49 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
51 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
53 glEnable(GL_TEXTURE_1D);
59 glDisable(GL_DEPTH_TEST);
61 glDepthMask(GL_FALSE);
64 glViewport(0, 0, width, 1);
66 glMatrixMode(GL_PROJECTION);
68 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
70 glMatrixMode(GL_MODELVIEW);
74 // Draw the texture stretched over a long quad, interpolating it out.
75 // Note that since the texel center is in (0.5), we need to adjust the
76 // texture coordinates in order not to get long stretches of (1,1,1,...)
77 // at the start and (...,0,0,0) at the end.
81 glVertex2f(0.0f, 0.0f);
84 glVertex2f(1.0f, 0.0f);
87 glVertex2f(1.0f, 1.0f);
90 glVertex2f(0.0f, 1.0f);
95 glDisable(GL_TEXTURE_1D);
98 // Now read the data back and see what the card did.
99 // (We only look at the red channel; the others will surely be the same.)
100 // We assume a linear ramp; anything else will give sort of odd results here.
101 float out_data[width];
102 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
105 float biggest_jump = 0.0f;
106 for (unsigned i = 1; i < width; ++i) {
107 assert(out_data[i] >= out_data[i - 1]);
108 biggest_jump = std::max(biggest_jump, out_data[i] - out_data[i - 1]);
111 movit_texel_subpixel_precision = biggest_jump;
114 glBindTexture(GL_TEXTURE_1D, 0);
116 glBindFramebuffer(GL_FRAMEBUFFER, 0);
118 glDeleteFramebuffers(1, &fbo);
120 glDeleteTextures(1, &dst_texnum);
122 glDeleteTextures(1, &src_texnum);
126 void get_extensions(std::set<std::string> *extensions)
128 char *str = strdup(reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)));
129 for (char *ptr = strtok(str, " "); ptr != NULL; ptr = strtok(NULL, " ")) {
130 extensions->insert(ptr);
135 void check_extensions()
137 std::set<std::string> extensions;
138 get_extensions(&extensions);
140 // We fundamentally need FBOs and floating-point textures.
141 assert(extensions.count("GL_ARB_framebuffer_object") != 0);
142 assert(extensions.count("GL_ARB_texture_float") != 0);
144 // We assume that we can use non-power-of-two textures without restrictions.
145 assert(extensions.count("GL_ARB_texture_non_power_of_two") != 0);
147 // We also need GLSL fragment shaders.
148 assert(extensions.count("GL_ARB_fragment_shader") != 0);
149 assert(extensions.count("GL_ARB_shading_language_100") != 0);
151 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
152 // but no modern card would really not provide it.)
153 assert(extensions.count("GL_ARB_pixel_buffer_object") != 0);
155 // ResampleEffect uses RG textures to encode a two-component LUT.
156 assert(extensions.count("GL_ARB_texture_rg") != 0);
158 // sRGB texture decode would be nice, but are not mandatory
159 // (GammaExpansionEffect can do the same thing if needed).
160 movit_srgb_textures_supported = extensions.count("GL_EXT_texture_sRGB");
167 if (movit_initialized) {
172 glPixelStorei(GL_PACK_ALIGNMENT, 1);
173 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
175 measure_texel_subpixel_precision();
178 movit_initialized = true;