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 bool check_extensions()
283 // GLES generally doesn't use extensions as actively as desktop OpenGL.
284 // For now, we say that for GLES, we require GLES 3, which has everything
287 // Since we use implicit #version 100, we don't have round(). We will
288 // fix this at some later stage.
289 if (!epoxy_is_desktop_gl()) {
290 if (epoxy_gl_version() >= 30) {
291 movit_srgb_textures_supported = true;
292 movit_shader_rounding_supported = false;
298 // We fundamentally need FBOs and floating-point textures.
299 // FBOs are covered by OpenGL 1.5, and are not an extension there.
300 // Floating-point textures are part of OpenGL 3.0 and newer.
301 if (epoxy_gl_version() < 15 &&
302 !epoxy_has_gl_extension("GL_ARB_framebuffer_object")) return false;
303 if (epoxy_gl_version() < 30 &&
304 !epoxy_has_gl_extension("GL_ARB_texture_float")) return false;
306 // We assume that we can use non-power-of-two textures without restrictions.
307 if (epoxy_gl_version() < 20 &&
308 !epoxy_has_gl_extension("GL_ARB_texture_non_power_of_two")) return false;
310 // We also need GLSL fragment shaders.
311 if (epoxy_gl_version() < 20) {
312 if (!epoxy_has_gl_extension("GL_ARB_fragment_shader")) return false;
313 if (!epoxy_has_gl_extension("GL_ARB_shading_language_100")) return false;
316 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
317 // but no modern card would really not provide it.)
318 if (epoxy_gl_version() < 21 &&
319 !epoxy_has_gl_extension("GL_ARB_pixel_buffer_object")) return false;
321 // ResampleEffect uses RG textures to encode a two-component LUT.
322 if (epoxy_gl_version() < 30 &&
323 !epoxy_has_gl_extension("GL_ARB_texture_rg")) return false;
325 // sRGB texture decode would be nice, but are not mandatory
326 // (GammaExpansionEffect can do the same thing if needed).
327 movit_srgb_textures_supported =
328 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
330 // We may want to use round() at the end of the final shader,
331 // if supported. We need either GLSL 1.30 or this extension to do that,
332 // and 1.30 brings with it other things that we don't want to demand
334 movit_shader_rounding_supported =
335 (epoxy_gl_version() >= 30 || epoxy_has_gl_extension("GL_EXT_gpu_shader4"));
340 double get_glsl_version()
342 char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
344 // Skip past the first period.
345 char *ptr = strchr(glsl_version_str, '.');
349 // Now cut the string off at the next period or space, whatever comes first
350 // (unless the string ends first).
351 while (*ptr && *ptr != '.' && *ptr != ' ') {
356 // Now we have something on the form X.YY. We convert it to a float, and hope
357 // that if it's inexact (e.g. 1.30), atof() will round the same way the
359 float glsl_version = atof(glsl_version_str);
360 free(glsl_version_str);
365 void APIENTRY debug_callback(GLenum source,
371 const void *userParam)
373 printf("Debug: %s\n", message);
378 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
380 if (movit_initialized) {
384 movit_data_directory = new string(data_directory);
385 movit_debug_level = debug_level;
388 glPixelStorei(GL_PACK_ALIGNMENT, 1);
389 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
390 glDisable(GL_DITHER);
392 // You can turn this on if you want detailed debug messages from the driver.
393 // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
394 // or you might not get much data back.
395 // glDebugMessageCallbackARB(callback, NULL);
396 // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
398 if (!check_extensions()) {
402 // Find out what shader model we should compile for.
403 if (epoxy_is_desktop_gl()) {
404 if (get_glsl_version() >= 1.30) {
405 movit_shader_model = MOVIT_GLSL_130;
407 movit_shader_model = MOVIT_GLSL_110;
410 movit_shader_model = MOVIT_ESSL_300;
413 measure_texel_subpixel_precision();
414 measure_roundoff_problems();
416 movit_initialized = true;