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 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 vector<string> frag_shader_outputs;
82 GLuint glsl_program_num = resource_pool.compile_glsl_program(
83 read_version_dependent_file("vs", "vert"),
84 read_version_dependent_file("texture1d", "frag"),
86 glUseProgram(glsl_program_num);
88 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
91 // Draw the texture stretched over a long quad, interpolating it out.
92 // Note that since the texel center is in (0.5), we need to adjust the
93 // texture coordinates in order not to get long stretches of (1,1,1,...)
94 // at the start and (...,0,0,0) at the end.
101 float texcoords[] = {
109 glGenVertexArrays(1, &vao);
111 glBindVertexArray(vao);
114 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
115 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(texcoords), texcoords);
117 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
120 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
121 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
126 // Now read the data back and see what the card did.
127 // (We only look at the red channel; the others will surely be the same.)
128 // We assume a linear ramp; anything else will give sort of odd results here.
129 float out_data[width * 4];
130 glReadPixels(0, 0, width, 1, GL_RGBA, GL_FLOAT, out_data);
133 float biggest_jump = 0.0f;
134 for (unsigned i = 1; i < width; ++i) {
135 assert(out_data[i * 4] >= out_data[(i - 1) * 4]);
136 biggest_jump = max(biggest_jump, out_data[i * 4] - out_data[(i - 1) * 4]);
139 assert(biggest_jump > 0.0);
140 movit_texel_subpixel_precision = biggest_jump;
143 glBindTexture(GL_TEXTURE_2D, 0);
145 glBindFramebuffer(GL_FRAMEBUFFER, 0);
147 glDeleteFramebuffers(1, &fbo);
149 glDeleteTextures(1, &dst_texnum);
151 glDeleteTextures(1, &src_texnum);
154 resource_pool.release_glsl_program(glsl_program_num);
155 glDeleteVertexArrays(1, &vao);
159 void measure_roundoff_problems()
161 ResourcePool resource_pool;
163 // Generate a destination texture to render to, and an FBO.
164 GLuint dst_texnum, fbo;
166 glGenTextures(1, &dst_texnum);
168 glBindTexture(GL_TEXTURE_2D, dst_texnum);
170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
173 glGenFramebuffers(1, &fbo);
175 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
177 glFramebufferTexture2D(
179 GL_COLOR_ATTACHMENT0,
185 // Now generate a texture where every value except the last should be
186 // rounded up to the next one. However, there are cards (in highly
187 // common use) that can't do this right, for unknown reasons.
190 for (int i = 0; i < 256; ++i) {
191 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
192 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
194 glGenTextures(1, &src_texnum);
196 glBindTexture(GL_TEXTURE_2D, src_texnum);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
202 glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
208 glDisable(GL_DEPTH_TEST);
210 glDepthMask(GL_FALSE);
213 glViewport(0, 0, 512, 1);
215 vector<string> frag_shader_outputs;
216 GLuint glsl_program_num = resource_pool.compile_glsl_program(
217 read_version_dependent_file("vs", "vert"),
218 read_version_dependent_file("texture1d", "frag"),
219 frag_shader_outputs);
220 glUseProgram(glsl_program_num);
222 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
224 // Draw the texture stretched over a long quad, interpolating it out.
233 glGenVertexArrays(1, &vao);
235 glBindVertexArray(vao);
238 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
239 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same data.
241 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
244 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
245 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
250 // Now read the data back and see what the card did. (Ignore the last value.)
251 // (We only look at the red channel; the others will surely be the same.)
252 unsigned char out_data[512 * 4];
253 glReadPixels(0, 0, 512, 1, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
256 int wrongly_rounded = 0;
257 for (unsigned i = 0; i < 255; ++i) {
258 if (out_data[(i * 2 + 0) * 4] != i) {
261 if (out_data[(i * 2 + 1) * 4] != i + 1) {
266 movit_num_wrongly_rounded = wrongly_rounded;
269 glBindTexture(GL_TEXTURE_2D, 0);
271 glBindFramebuffer(GL_FRAMEBUFFER, 0);
273 glDeleteFramebuffers(1, &fbo);
275 glDeleteTextures(1, &dst_texnum);
277 glDeleteTextures(1, &src_texnum);
280 resource_pool.release_glsl_program(glsl_program_num);
281 glDeleteVertexArrays(1, &vao);
285 struct RequiredExtension {
286 int min_equivalent_gl_version;
287 const char extension_name[64];
289 const RequiredExtension required_extensions[] = {
290 // We fundamentally need FBOs and floating-point textures.
291 // FBOs are covered by OpenGL 1.5, and are not an extension there.
292 // Floating-point textures are part of OpenGL 3.0 and newer.
293 { 15, "GL_ARB_framebuffer_object" },
294 { 30, "GL_ARB_texture_float" },
296 // We assume that we can use non-power-of-two textures without restrictions.
297 { 20, "GL_ARB_texture_non_power_of_two" },
299 // We also need GLSL fragment shaders.
300 { 20, "GL_ARB_fragment_shader" },
301 { 20, "GL_ARB_shading_language_100" },
303 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
304 // but no modern card would really not provide it.)
305 { 21, "GL_ARB_pixel_buffer_object" },
307 // ResampleEffect uses RG textures to encode a two-component LUT.
308 // We also need GL_R several places, for single-channel input.
309 { 30, "GL_ARB_texture_rg" },
312 bool check_extensions()
314 // GLES generally doesn't use extensions as actively as desktop OpenGL.
315 // For now, we say that for GLES, we require GLES 3, which has everything
317 if (!epoxy_is_desktop_gl()) {
318 if (epoxy_gl_version() >= 30) {
319 movit_srgb_textures_supported = true;
322 fprintf(stderr, "Movit system requirements: GLES version %.1f is too old (GLES 3.0 needed).\n",
323 0.1f * epoxy_gl_version());
324 fprintf(stderr, "Movit initialization failed.\n");
329 // Check all extensions, and output errors for the ones that we are missing.
331 int gl_version = epoxy_gl_version();
333 for (unsigned i = 0; i < sizeof(required_extensions) / sizeof(required_extensions[0]); ++i) {
334 if (gl_version < required_extensions[i].min_equivalent_gl_version &&
335 !epoxy_has_gl_extension(required_extensions[i].extension_name)) {
336 fprintf(stderr, "Movit system requirements: Needs extension '%s' or at least OpenGL version %.1f (has version %.1f)\n",
337 required_extensions[i].extension_name,
338 0.1f * required_extensions[i].min_equivalent_gl_version,
345 fprintf(stderr, "Movit initialization failed.\n");
349 // sRGB texture decode would be nice, but are not mandatory
350 // (GammaExpansionEffect can do the same thing if needed).
351 movit_srgb_textures_supported =
352 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
354 // The user can specify that they want a timing report for each
355 // phase in an effect chain. However, that depends on this extension;
356 // without it, we do cannot even create the query objects.
357 movit_timer_queries_supported =
358 (epoxy_gl_version() >= 33 || epoxy_has_gl_extension("GL_ARB_timer_query"));
363 double get_glsl_version()
365 char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
367 // Skip past the first period.
368 char *ptr = strchr(glsl_version_str, '.');
372 // Now cut the string off at the next period or space, whatever comes first
373 // (unless the string ends first).
374 while (*ptr && *ptr != '.' && *ptr != ' ') {
379 // Now we have something on the form X.YY. We convert it to a float, and hope
380 // that if it's inexact (e.g. 1.30), atof() will round the same way the
382 float glsl_version = atof(glsl_version_str);
383 free(glsl_version_str);
388 void APIENTRY debug_callback(GLenum source,
394 const void *userParam)
396 printf("Debug: %s\n", message);
401 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
403 if (movit_initialized) {
407 movit_data_directory = new string(data_directory);
408 movit_debug_level = debug_level;
411 glPixelStorei(GL_PACK_ALIGNMENT, 1);
412 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
413 glDisable(GL_DITHER);
415 // You can turn this on if you want detailed debug messages from the driver.
416 // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
417 // or you might not get much data back.
418 // glDebugMessageCallbackARB(callback, NULL);
419 // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
421 if (!check_extensions()) {
425 // Find out what shader model we should compile for.
426 // We need at least 1.30, due to use of (among others) integers.
427 if (epoxy_is_desktop_gl()) {
428 if (get_glsl_version() < 1.30f) {
429 fprintf(stderr, "Movit system requirements: Needs at least GLSL version 1.30 (has version %.1f)\n",
433 if (get_glsl_version() < 1.50f) {
434 movit_shader_model = MOVIT_GLSL_130;
436 // Note: All of our 1.50 shaders are identical to our 1.30 shaders,
437 // but OS X does not support 1.30; only 1.10 (which we don't support
438 // anymore) and 1.50 (and then only with core contexts). So we keep
439 // a second set of shaders around whose only difference is the different
440 // #version declaration.
441 movit_shader_model = MOVIT_GLSL_150;
444 movit_shader_model = MOVIT_ESSL_300;
447 measure_texel_subpixel_precision();
448 measure_roundoff_problems();
450 movit_initialized = true;