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;
21 MovitShaderModel movit_shader_model;
23 // The rules for objects with nontrivial constructors in static scope
24 // are somewhat convoluted, and easy to mess up. We simply have a
25 // pointer instead (and never care to clean it up).
26 string *movit_data_directory = NULL;
30 void measure_texel_subpixel_precision()
32 ResourcePool resource_pool;
33 static const unsigned width = 4096;
35 // Generate a destination texture to render to, and an FBO.
36 GLuint dst_texnum, fbo;
38 glGenTextures(1, &dst_texnum);
40 glBindTexture(GL_TEXTURE_2D, dst_texnum);
42 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, 1, 0, GL_RGBA, GL_FLOAT, NULL);
45 glGenFramebuffers(1, &fbo);
47 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
49 glFramebufferTexture2D(
57 // Now generate a simple texture that's just [0,1].
59 float texdata[] = { 0, 1 };
60 glGenTextures(1, &src_texnum);
62 glBindTexture(GL_TEXTURE_2D, src_texnum);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
68 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, 2, 1, 0, GL_RED, GL_FLOAT, texdata);
74 glDisable(GL_DEPTH_TEST);
76 glDepthMask(GL_FALSE);
79 glViewport(0, 0, width, 1);
81 GLuint glsl_program_num = resource_pool.compile_glsl_program(
82 read_version_dependent_file("vs", "vert"),
83 read_version_dependent_file("texture1d", "frag"));
84 glUseProgram(glsl_program_num);
86 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
89 // Draw the texture stretched over a long quad, interpolating it out.
90 // Note that since the texel center is in (0.5), we need to adjust the
91 // texture coordinates in order not to get long stretches of (1,1,1,...)
92 // at the start and (...,0,0,0) at the end.
107 glGenVertexArrays(1, &vao);
109 glBindVertexArray(vao);
112 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
113 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(texcoords), texcoords);
115 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
118 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
119 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
124 // Now read the data back and see what the card did.
125 // (We only look at the red channel; the others will surely be the same.)
126 // We assume a linear ramp; anything else will give sort of odd results here.
127 float out_data[width * 4];
128 glReadPixels(0, 0, width, 1, GL_RGBA, GL_FLOAT, out_data);
131 float biggest_jump = 0.0f;
132 for (unsigned i = 1; i < width; ++i) {
133 assert(out_data[i * 4] >= out_data[(i - 1) * 4]);
134 biggest_jump = max(biggest_jump, out_data[i * 4] - out_data[(i - 1) * 4]);
137 assert(biggest_jump > 0.0);
138 movit_texel_subpixel_precision = biggest_jump;
141 glBindTexture(GL_TEXTURE_2D, 0);
143 glBindFramebuffer(GL_FRAMEBUFFER, 0);
145 glDeleteFramebuffers(1, &fbo);
147 glDeleteTextures(1, &dst_texnum);
149 glDeleteTextures(1, &src_texnum);
152 resource_pool.release_glsl_program(glsl_program_num);
153 glDeleteVertexArrays(1, &vao);
157 void measure_roundoff_problems()
159 ResourcePool resource_pool;
161 // Generate a destination texture to render to, and an FBO.
162 GLuint dst_texnum, fbo;
164 glGenTextures(1, &dst_texnum);
166 glBindTexture(GL_TEXTURE_2D, dst_texnum);
168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
171 glGenFramebuffers(1, &fbo);
173 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
175 glFramebufferTexture2D(
177 GL_COLOR_ATTACHMENT0,
183 // Now generate a texture where every value except the last should be
184 // rounded up to the next one. However, there are cards (in highly
185 // common use) that can't do this right, for unknown reasons.
188 for (int i = 0; i < 256; ++i) {
189 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
190 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
192 glGenTextures(1, &src_texnum);
194 glBindTexture(GL_TEXTURE_2D, src_texnum);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
200 glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
206 glDisable(GL_DEPTH_TEST);
208 glDepthMask(GL_FALSE);
211 glViewport(0, 0, 512, 1);
213 GLuint glsl_program_num = resource_pool.compile_glsl_program(
214 read_version_dependent_file("vs", "vert"),
215 read_version_dependent_file("texture1d", "frag"));
216 glUseProgram(glsl_program_num);
218 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
220 // Draw the texture stretched over a long quad, interpolating it out.
229 glGenVertexArrays(1, &vao);
231 glBindVertexArray(vao);
234 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
235 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same data.
237 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
240 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
241 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
246 // Now read the data back and see what the card did. (Ignore the last value.)
247 // (We only look at the red channel; the others will surely be the same.)
248 unsigned char out_data[512 * 4];
249 glReadPixels(0, 0, 512, 1, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
252 int wrongly_rounded = 0;
253 for (unsigned i = 0; i < 255; ++i) {
254 if (out_data[(i * 2 + 0) * 4] != i) {
257 if (out_data[(i * 2 + 1) * 4] != i + 1) {
262 movit_num_wrongly_rounded = wrongly_rounded;
265 glBindTexture(GL_TEXTURE_2D, 0);
267 glBindFramebuffer(GL_FRAMEBUFFER, 0);
269 glDeleteFramebuffers(1, &fbo);
271 glDeleteTextures(1, &dst_texnum);
273 glDeleteTextures(1, &src_texnum);
276 resource_pool.release_glsl_program(glsl_program_num);
277 glDeleteVertexArrays(1, &vao);
281 struct RequiredExtension {
282 int min_equivalent_gl_version;
283 const char extension_name[64];
285 const RequiredExtension required_extensions[] = {
286 // We fundamentally need FBOs and floating-point textures.
287 // FBOs are covered by OpenGL 1.5, and are not an extension there.
288 // Floating-point textures are part of OpenGL 3.0 and newer.
289 { 15, "GL_ARB_framebuffer_object" },
290 { 30, "GL_ARB_texture_float" },
292 // We assume that we can use non-power-of-two textures without restrictions.
293 { 20, "GL_ARB_texture_non_power_of_two" },
295 // We also need GLSL fragment shaders.
296 { 20, "GL_ARB_fragment_shader" },
297 { 20, "GL_ARB_shading_language_100" },
299 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
300 // but no modern card would really not provide it.)
301 { 21, "GL_ARB_pixel_buffer_object" },
303 // ResampleEffect uses RG textures to encode a two-component LUT.
304 // We also need GL_R several places, for single-channel input.
305 { 30, "GL_ARB_texture_rg" },
308 bool check_extensions()
310 // GLES generally doesn't use extensions as actively as desktop OpenGL.
311 // For now, we say that for GLES, we require GLES 3, which has everything
313 if (!epoxy_is_desktop_gl()) {
314 if (epoxy_gl_version() >= 30) {
315 movit_srgb_textures_supported = true;
316 movit_shader_rounding_supported = true;
319 fprintf(stderr, "Movit system requirements: GLES version %.1f is too old (GLES 3.0 needed).\n",
320 0.1f * epoxy_gl_version());
321 fprintf(stderr, "Movit initialization failed.\n");
326 // Check all extensions, and output errors for the ones that we are missing.
328 int gl_version = epoxy_gl_version();
330 for (unsigned i = 0; i < sizeof(required_extensions) / sizeof(required_extensions[0]); ++i) {
331 if (gl_version < required_extensions[i].min_equivalent_gl_version &&
332 !epoxy_has_gl_extension(required_extensions[i].extension_name)) {
333 fprintf(stderr, "Movit system requirements: Needs extension '%s' or at least OpenGL version %.1f (has version %.1f)\n",
334 required_extensions[i].extension_name,
335 0.1f * required_extensions[i].min_equivalent_gl_version,
342 fprintf(stderr, "Movit initialization failed.\n");
346 // sRGB texture decode would be nice, but are not mandatory
347 // (GammaExpansionEffect can do the same thing if needed).
348 movit_srgb_textures_supported =
349 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
351 // We may want to use round() at the end of the final shader,
352 // if supported. We need either GLSL 1.30 or this extension to do that,
353 // and 1.30 brings with it other things that we don't want to demand
355 movit_shader_rounding_supported =
356 (epoxy_gl_version() >= 30 || epoxy_has_gl_extension("GL_EXT_gpu_shader4"));
361 double get_glsl_version()
363 char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
365 // Skip past the first period.
366 char *ptr = strchr(glsl_version_str, '.');
370 // Now cut the string off at the next period or space, whatever comes first
371 // (unless the string ends first).
372 while (*ptr && *ptr != '.' && *ptr != ' ') {
377 // Now we have something on the form X.YY. We convert it to a float, and hope
378 // that if it's inexact (e.g. 1.30), atof() will round the same way the
380 float glsl_version = atof(glsl_version_str);
381 free(glsl_version_str);
386 void APIENTRY debug_callback(GLenum source,
392 const void *userParam)
394 printf("Debug: %s\n", message);
399 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
401 if (movit_initialized) {
405 movit_data_directory = new string(data_directory);
406 movit_debug_level = debug_level;
409 glPixelStorei(GL_PACK_ALIGNMENT, 1);
410 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
411 glDisable(GL_DITHER);
413 // You can turn this on if you want detailed debug messages from the driver.
414 // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
415 // or you might not get much data back.
416 // glDebugMessageCallbackARB(callback, NULL);
417 // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
419 if (!check_extensions()) {
423 // Find out what shader model we should compile for.
424 if (epoxy_is_desktop_gl()) {
425 if (get_glsl_version() >= 1.30) {
426 movit_shader_model = MOVIT_GLSL_130;
428 movit_shader_model = MOVIT_GLSL_110;
431 movit_shader_model = MOVIT_ESSL_300;
434 measure_texel_subpixel_precision();
435 measure_roundoff_problems();
437 movit_initialized = true;