5 bool movit_initialized = false;
6 float movit_texel_subpixel_precision;
10 void measure_texel_subpixel_precision()
12 static const unsigned width = 1024;
14 // Generate a destination texture to render to, and an FBO.
15 GLuint dst_texnum, fbo;
17 glGenTextures(1, &dst_texnum);
19 glBindTexture(GL_TEXTURE_2D, dst_texnum);
21 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
24 glGenFramebuffers(1, &fbo);
26 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
28 glFramebufferTexture2D(
36 // Now generate a simple texture that's just [0,1].
38 float texdata[] = { 0, 1 };
39 glGenTextures(1, &dst_texnum);
41 glBindTexture(GL_TEXTURE_1D, dst_texnum);
43 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
45 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
47 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
49 glEnable(GL_TEXTURE_1D);
55 glDisable(GL_DEPTH_TEST);
57 glDepthMask(GL_FALSE);
60 glViewport(0, 0, width, 1);
62 glMatrixMode(GL_PROJECTION);
64 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
66 glMatrixMode(GL_MODELVIEW);
70 // Draw the texture stretched over a long quad, interpolating it out.
71 // Note that since the texel center is in (0.5), we need to adjust the
72 // texture coordinates in order not to get long stretches of (1,1,1,...)
73 // at the start and (...,0,0,0) at the end.
77 glVertex2f(0.0f, 0.0f);
80 glVertex2f(1.0f, 0.0f);
83 glVertex2f(1.0f, 1.0f);
86 glVertex2f(0.0f, 1.0f);
91 glDisable(GL_TEXTURE_1D);
94 // Now read the data back and see what the card did.
95 // (We only look at the red channel; the others will surely be the same.)
96 // We assume a linear ramp; anything else will give sort of odd results here.
97 float out_data[width];
98 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
101 float biggest_jump = 0.0f;
102 for (unsigned i = 1; i < width; ++i) {
103 assert(out_data[i] >= out_data[i - 1]);
104 biggest_jump = std::max(biggest_jump, out_data[i] - out_data[i - 1]);
107 movit_texel_subpixel_precision = biggest_jump;
110 glBindTexture(GL_TEXTURE_1D, 0);
112 glBindFramebuffer(GL_FRAMEBUFFER, 0);
114 glDeleteFramebuffers(1, &fbo);
116 glDeleteTextures(1, &dst_texnum);
118 glDeleteTextures(1, &src_texnum);
126 if (movit_initialized) {
131 glPixelStorei(GL_PACK_ALIGNMENT, 1);
132 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
134 measure_texel_subpixel_precision();
136 movit_initialized = true;