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 bool movit_timer_queries_supported;
20 int movit_num_wrongly_rounded;
21 bool movit_shader_rounding_supported;
22 MovitShaderModel movit_shader_model;
24 // The rules for objects with nontrivial constructors in static scope
25 // are somewhat convoluted, and easy to mess up. We simply have a
26 // pointer instead (and never care to clean it up).
27 string *movit_data_directory = NULL;
31 void measure_texel_subpixel_precision()
33 ResourcePool resource_pool;
34 static const unsigned width = 4096;
36 // Generate a destination texture to render to, and an FBO.
37 GLuint dst_texnum, fbo;
39 glGenTextures(1, &dst_texnum);
41 glBindTexture(GL_TEXTURE_2D, dst_texnum);
43 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, 1, 0, GL_RGBA, GL_FLOAT, NULL);
46 glGenFramebuffers(1, &fbo);
48 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
50 glFramebufferTexture2D(
58 // Now generate a simple texture that's just [0,1].
60 float texdata[] = { 0, 1 };
61 glGenTextures(1, &src_texnum);
63 glBindTexture(GL_TEXTURE_2D, src_texnum);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, 2, 1, 0, GL_RED, GL_FLOAT, texdata);
75 glDisable(GL_DEPTH_TEST);
77 glDepthMask(GL_FALSE);
80 glViewport(0, 0, width, 1);
82 vector<string> frag_shader_outputs;
83 GLuint glsl_program_num = resource_pool.compile_glsl_program(
84 read_version_dependent_file("vs", "vert"),
85 read_version_dependent_file("texture1d", "frag"),
87 glUseProgram(glsl_program_num);
89 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
92 // Draw the texture stretched over a long quad, interpolating it out.
93 // Note that since the texel center is in (0.5), we need to adjust the
94 // texture coordinates in order not to get long stretches of (1,1,1,...)
95 // at the start and (...,0,0,0) at the end.
102 float texcoords[] = {
110 glGenVertexArrays(1, &vao);
112 glBindVertexArray(vao);
115 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
116 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(texcoords), texcoords);
118 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
121 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
122 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
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 * 4];
131 glReadPixels(0, 0, width, 1, GL_RGBA, GL_FLOAT, out_data);
134 float biggest_jump = 0.0f;
135 for (unsigned i = 1; i < width; ++i) {
136 assert(out_data[i * 4] >= out_data[(i - 1) * 4]);
137 biggest_jump = max(biggest_jump, out_data[i * 4] - out_data[(i - 1) * 4]);
140 assert(biggest_jump > 0.0);
141 movit_texel_subpixel_precision = biggest_jump;
144 glBindTexture(GL_TEXTURE_2D, 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);
156 glDeleteVertexArrays(1, &vao);
160 void measure_roundoff_problems()
162 ResourcePool resource_pool;
164 // Generate a destination texture to render to, and an FBO.
165 GLuint dst_texnum, fbo;
167 glGenTextures(1, &dst_texnum);
169 glBindTexture(GL_TEXTURE_2D, dst_texnum);
171 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
174 glGenFramebuffers(1, &fbo);
176 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
178 glFramebufferTexture2D(
180 GL_COLOR_ATTACHMENT0,
186 // Now generate a texture where every value except the last should be
187 // rounded up to the next one. However, there are cards (in highly
188 // common use) that can't do this right, for unknown reasons.
191 for (int i = 0; i < 256; ++i) {
192 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
193 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
195 glGenTextures(1, &src_texnum);
197 glBindTexture(GL_TEXTURE_2D, src_texnum);
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
203 glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
209 glDisable(GL_DEPTH_TEST);
211 glDepthMask(GL_FALSE);
214 glViewport(0, 0, 512, 1);
216 vector<string> frag_shader_outputs;
217 GLuint glsl_program_num = resource_pool.compile_glsl_program(
218 read_version_dependent_file("vs", "vert"),
219 read_version_dependent_file("texture1d", "frag"),
220 frag_shader_outputs);
221 glUseProgram(glsl_program_num);
223 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
225 // Draw the texture stretched over a long quad, interpolating it out.
234 glGenVertexArrays(1, &vao);
236 glBindVertexArray(vao);
239 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
240 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same data.
242 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
245 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
246 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
251 // Now read the data back and see what the card did. (Ignore the last value.)
252 // (We only look at the red channel; the others will surely be the same.)
253 unsigned char out_data[512 * 4];
254 glReadPixels(0, 0, 512, 1, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
257 int wrongly_rounded = 0;
258 for (unsigned i = 0; i < 255; ++i) {
259 if (out_data[(i * 2 + 0) * 4] != i) {
262 if (out_data[(i * 2 + 1) * 4] != i + 1) {
267 movit_num_wrongly_rounded = wrongly_rounded;
270 glBindTexture(GL_TEXTURE_2D, 0);
272 glBindFramebuffer(GL_FRAMEBUFFER, 0);
274 glDeleteFramebuffers(1, &fbo);
276 glDeleteTextures(1, &dst_texnum);
278 glDeleteTextures(1, &src_texnum);
281 resource_pool.release_glsl_program(glsl_program_num);
282 glDeleteVertexArrays(1, &vao);
286 struct RequiredExtension {
287 int min_equivalent_gl_version;
288 const char extension_name[64];
290 const RequiredExtension required_extensions[] = {
291 // We fundamentally need FBOs and floating-point textures.
292 // FBOs are covered by OpenGL 1.5, and are not an extension there.
293 // Floating-point textures are part of OpenGL 3.0 and newer.
294 { 15, "GL_ARB_framebuffer_object" },
295 { 30, "GL_ARB_texture_float" },
297 // We assume that we can use non-power-of-two textures without restrictions.
298 { 20, "GL_ARB_texture_non_power_of_two" },
300 // We also need GLSL fragment shaders.
301 { 20, "GL_ARB_fragment_shader" },
302 { 20, "GL_ARB_shading_language_100" },
304 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
305 // but no modern card would really not provide it.)
306 { 21, "GL_ARB_pixel_buffer_object" },
308 // ResampleEffect uses RG textures to encode a two-component LUT.
309 // We also need GL_R several places, for single-channel input.
310 { 30, "GL_ARB_texture_rg" },
313 bool check_extensions()
315 // GLES generally doesn't use extensions as actively as desktop OpenGL.
316 // For now, we say that for GLES, we require GLES 3, which has everything
318 if (!epoxy_is_desktop_gl()) {
319 if (epoxy_gl_version() >= 30) {
320 movit_srgb_textures_supported = true;
321 movit_shader_rounding_supported = true;
324 fprintf(stderr, "Movit system requirements: GLES version %.1f is too old (GLES 3.0 needed).\n",
325 0.1f * epoxy_gl_version());
326 fprintf(stderr, "Movit initialization failed.\n");
331 // Check all extensions, and output errors for the ones that we are missing.
333 int gl_version = epoxy_gl_version();
335 for (unsigned i = 0; i < sizeof(required_extensions) / sizeof(required_extensions[0]); ++i) {
336 if (gl_version < required_extensions[i].min_equivalent_gl_version &&
337 !epoxy_has_gl_extension(required_extensions[i].extension_name)) {
338 fprintf(stderr, "Movit system requirements: Needs extension '%s' or at least OpenGL version %.1f (has version %.1f)\n",
339 required_extensions[i].extension_name,
340 0.1f * required_extensions[i].min_equivalent_gl_version,
347 fprintf(stderr, "Movit initialization failed.\n");
351 // sRGB texture decode would be nice, but are not mandatory
352 // (GammaExpansionEffect can do the same thing if needed).
353 movit_srgb_textures_supported =
354 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
356 // We may want to use round() at the end of the final shader,
357 // if supported. We need either GLSL 1.30 or this extension to do that,
358 // and 1.30 brings with it other things that we don't want to demand
360 movit_shader_rounding_supported =
361 (epoxy_gl_version() >= 30 || epoxy_has_gl_extension("GL_EXT_gpu_shader4"));
363 // The user can specify that they want a timing report for each
364 // phase in an effect chain. However, that depends on this extension;
365 // without it, we do cannot even create the query objects.
366 movit_timer_queries_supported =
367 (epoxy_gl_version() >= 33 || epoxy_has_gl_extension("GL_ARB_timer_query"));
372 double get_glsl_version()
374 char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
376 // Skip past the first period.
377 char *ptr = strchr(glsl_version_str, '.');
381 // Now cut the string off at the next period or space, whatever comes first
382 // (unless the string ends first).
383 while (*ptr && *ptr != '.' && *ptr != ' ') {
388 // Now we have something on the form X.YY. We convert it to a float, and hope
389 // that if it's inexact (e.g. 1.30), atof() will round the same way the
391 float glsl_version = atof(glsl_version_str);
392 free(glsl_version_str);
397 void APIENTRY debug_callback(GLenum source,
403 const void *userParam)
405 printf("Debug: %s\n", message);
410 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
412 if (movit_initialized) {
416 movit_data_directory = new string(data_directory);
417 movit_debug_level = debug_level;
420 glPixelStorei(GL_PACK_ALIGNMENT, 1);
421 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
422 glDisable(GL_DITHER);
424 // You can turn this on if you want detailed debug messages from the driver.
425 // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
426 // or you might not get much data back.
427 // glDebugMessageCallbackARB(callback, NULL);
428 // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
430 if (!check_extensions()) {
434 // Find out what shader model we should compile for.
435 // We need at least 1.30, due to use of (among others) integers.
436 if (epoxy_is_desktop_gl()) {
437 if (get_glsl_version() < 1.30f) {
438 fprintf(stderr, "Movit system requirements: Needs at least GLSL version 1.30 (has version %.1f)\n",
440 if (get_glsl_version() >= 1.10f) {
441 fprintf(stderr, "Attempting to continue nevertheless; expect shader compilation issues.\n");
442 fprintf(stderr, "Try switching to a core OpenGL context, as especially OS X drivers\n");
443 fprintf(stderr, "support newer GLSL versions there.\n");
444 movit_shader_model = MOVIT_GLSL_130_AS_110;
449 if (get_glsl_version() < 1.50f) {
450 movit_shader_model = MOVIT_GLSL_130;
452 // Note: All of our 1.50 shaders are identical to our 1.30 shaders,
453 // but OS X does not support 1.30; only 1.10 (which we don't support
454 // anymore) and 1.50 (and then only with core contexts). So we keep
455 // a second set of shaders around whose only difference is the different
456 // #version declaration.
457 movit_shader_model = MOVIT_GLSL_150;
460 movit_shader_model = MOVIT_ESSL_300;
463 measure_texel_subpixel_precision();
464 measure_roundoff_problems();
466 movit_initialized = true;