8 #include "resource_pool.h"
15 bool movit_initialized = false;
16 MovitDebugLevel movit_debug_level = MOVIT_DEBUG_ON;
17 float movit_texel_subpixel_precision;
18 bool movit_srgb_textures_supported;
19 int movit_num_wrongly_rounded;
20 bool movit_shader_rounding_supported;
22 // The rules for objects with nontrivial constructors in static scope
23 // are somewhat convoluted, and easy to mess up. We simply have a
24 // pointer instead (and never care to clean it up).
25 string *movit_data_directory = NULL;
29 void measure_texel_subpixel_precision()
31 ResourcePool resource_pool;
32 static const unsigned width = 4096;
34 // Generate a destination texture to render to, and an FBO.
35 GLuint dst_texnum, fbo;
37 glGenTextures(1, &dst_texnum);
39 glBindTexture(GL_TEXTURE_2D, dst_texnum);
41 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
44 glGenFramebuffers(1, &fbo);
46 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
48 glFramebufferTexture2D(
56 // Now generate a simple texture that's just [0,1].
58 float texdata[] = { 0, 1 };
59 glGenTextures(1, &src_texnum);
61 glBindTexture(GL_TEXTURE_1D, src_texnum);
63 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
67 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
73 glDisable(GL_DEPTH_TEST);
75 glDepthMask(GL_FALSE);
78 glViewport(0, 0, width, 1);
80 GLuint glsl_program_num = resource_pool.compile_glsl_program(
81 read_file("vs.vert"), read_file("texture1d.frag"));
82 glUseProgram(glsl_program_num);
84 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 1D sampler.
87 // Draw the texture stretched over a long quad, interpolating it out.
88 // Note that since the texel center is in (0.5), we need to adjust the
89 // texture coordinates in order not to get long stretches of (1,1,1,...)
90 // at the start and (...,0,0,0) at the end.
104 int position_attrib = glGetAttribLocation(glsl_program_num, "position");
105 assert(position_attrib != -1);
106 int texcoord_attrib = glGetAttribLocation(glsl_program_num, "texcoord");
107 assert(texcoord_attrib != -1);
108 glEnableVertexAttribArray(position_attrib);
110 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
112 glEnableVertexAttribArray(texcoord_attrib);
114 glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
117 glDrawArrays(GL_QUADS, 0, 4);
122 glDisableVertexAttribArray(position_attrib);
124 glDisableVertexAttribArray(texcoord_attrib);
127 // Now read the data back and see what the card did.
128 // (We only look at the red channel; the others will surely be the same.)
129 // We assume a linear ramp; anything else will give sort of odd results here.
130 float out_data[width];
131 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
134 float biggest_jump = 0.0f;
135 for (unsigned i = 1; i < width; ++i) {
136 assert(out_data[i] >= out_data[i - 1]);
137 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
140 assert(biggest_jump > 0.0);
141 movit_texel_subpixel_precision = biggest_jump;
144 glBindTexture(GL_TEXTURE_1D, 0);
146 glBindFramebuffer(GL_FRAMEBUFFER, 0);
148 glDeleteFramebuffers(1, &fbo);
150 glDeleteTextures(1, &dst_texnum);
152 glDeleteTextures(1, &src_texnum);
155 resource_pool.release_glsl_program(glsl_program_num);
158 void measure_roundoff_problems()
160 ResourcePool resource_pool;
162 // Generate a destination texture to render to, and an FBO.
163 GLuint dst_texnum, fbo;
165 glGenTextures(1, &dst_texnum);
167 glBindTexture(GL_TEXTURE_2D, dst_texnum);
169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
172 glGenFramebuffers(1, &fbo);
174 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
176 glFramebufferTexture2D(
178 GL_COLOR_ATTACHMENT0,
184 // Now generate a texture where every value except the last should be
185 // rounded up to the next one. However, there are cards (in highly
186 // common use) that can't do this right, for unknown reasons.
189 for (int i = 0; i < 256; ++i) {
190 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
191 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
193 glGenTextures(1, &src_texnum);
195 glBindTexture(GL_TEXTURE_1D, src_texnum);
197 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
199 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
201 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, 512, 0, GL_LUMINANCE, GL_FLOAT, texdata);
207 glDisable(GL_DEPTH_TEST);
209 glDepthMask(GL_FALSE);
212 glViewport(0, 0, 512, 1);
214 GLuint glsl_program_num = resource_pool.compile_glsl_program(
215 read_file("vs.vert"), read_file("texture1d.frag"));
216 glUseProgram(glsl_program_num);
218 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 1D sampler.
220 // Draw the texture stretched over a long quad, interpolating it out.
227 float texcoords[] = {
233 int position_attrib = glGetAttribLocation(glsl_program_num, "position");
234 assert(position_attrib != -1);
235 int texcoord_attrib = glGetAttribLocation(glsl_program_num, "texcoord");
236 assert(texcoord_attrib != -1);
237 glEnableVertexAttribArray(position_attrib);
239 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
241 glEnableVertexAttribArray(texcoord_attrib);
243 glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
245 glDrawArrays(GL_QUADS, 0, 4);
249 glDisableVertexAttribArray(position_attrib);
251 glDisableVertexAttribArray(texcoord_attrib);
253 // Now read the data back and see what the card did. (Ignore the last value.)
254 // (We only look at the red channel; the others will surely be the same.)
255 unsigned char out_data[512];
256 glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
259 int wrongly_rounded = 0;
260 for (unsigned i = 0; i < 255; ++i) {
261 if (out_data[i * 2 + 0] != i) {
264 if (out_data[i * 2 + 1] != i + 1) {
269 movit_num_wrongly_rounded = wrongly_rounded;
272 glBindTexture(GL_TEXTURE_1D, 0);
274 glBindFramebuffer(GL_FRAMEBUFFER, 0);
276 glDeleteFramebuffers(1, &fbo);
278 glDeleteTextures(1, &dst_texnum);
280 glDeleteTextures(1, &src_texnum);
283 resource_pool.release_glsl_program(glsl_program_num);
286 bool check_extensions()
288 // We fundamentally need FBOs and floating-point textures.
289 if (!glewIsSupported("GL_ARB_framebuffer_object")) return false;
290 if (!glewIsSupported("GL_ARB_texture_float")) return false;
292 // We assume that we can use non-power-of-two textures without restrictions.
293 if (!glewIsSupported("GL_ARB_texture_non_power_of_two")) return false;
295 // We also need GLSL fragment shaders.
296 if (!glewIsSupported("GL_ARB_fragment_shader")) return false;
297 if (!glewIsSupported("GL_ARB_shading_language_100")) return false;
299 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
300 // but no modern card would really not provide it.)
301 if (!glewIsSupported("GL_ARB_pixel_buffer_object")) return false;
303 // ResampleEffect uses RG textures to encode a two-component LUT.
304 if (!glewIsSupported("GL_ARB_texture_rg")) return false;
306 // sRGB texture decode would be nice, but are not mandatory
307 // (GammaExpansionEffect can do the same thing if needed).
308 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
310 // We may want to use round() at the end of the final shader,
311 // if supported. We need either GLSL 1.30 or this extension to do that,
312 // and 1.30 brings with it other things that we don't want to demand
314 movit_shader_rounding_supported = glewIsSupported("GL_EXT_gpu_shader4");
321 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
323 if (movit_initialized) {
327 movit_data_directory = new string(data_directory);
328 movit_debug_level = debug_level;
330 GLenum err = glewInit();
331 if (err != GLEW_OK) {
336 glPixelStorei(GL_PACK_ALIGNMENT, 1);
337 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
338 glDisable(GL_DITHER);
340 if (!check_extensions()) {
343 measure_texel_subpixel_precision();
344 measure_roundoff_problems();
346 movit_initialized = true;