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_timer_queries_supported;
19 int movit_num_wrongly_rounded;
20 MovitShaderModel movit_shader_model;
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, width, 1, 0, GL_RGBA, GL_FLOAT, 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_2D, src_texnum);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
67 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, 2, 1, 0, GL_RED, GL_FLOAT, texdata);
73 glDisable(GL_DEPTH_TEST);
75 glDepthMask(GL_FALSE);
78 glViewport(0, 0, width, 1);
80 vector<string> frag_shader_outputs;
81 GLuint glsl_program_num = resource_pool.compile_glsl_program(
82 read_version_dependent_file("vs", "vert"),
83 read_version_dependent_file("texture1d", "frag"),
85 glUseProgram(glsl_program_num);
87 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
90 // Draw the texture stretched over a long quad, interpolating it out.
91 // Note that since the texel center is in (0.5), we need to adjust the
92 // texture coordinates in order not to get long stretches of (1,1,1,...)
93 // at the start and (...,0,0,0) at the end.
100 float texcoords[] = {
108 glGenVertexArrays(1, &vao);
110 glBindVertexArray(vao);
113 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
114 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(texcoords), texcoords);
116 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
119 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
120 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
125 // Now read the data back and see what the card did.
126 // (We only look at the red channel; the others will surely be the same.)
127 // We assume a linear ramp; anything else will give sort of odd results here.
128 float out_data[width * 4];
129 glReadPixels(0, 0, width, 1, GL_RGBA, GL_FLOAT, out_data);
132 float biggest_jump = 0.0f;
133 for (unsigned i = 1; i < width; ++i) {
134 assert(out_data[i * 4] >= out_data[(i - 1) * 4]);
135 biggest_jump = max(biggest_jump, out_data[i * 4] - out_data[(i - 1) * 4]);
138 assert(biggest_jump > 0.0);
139 movit_texel_subpixel_precision = biggest_jump;
142 glBindTexture(GL_TEXTURE_2D, 0);
144 glBindFramebuffer(GL_FRAMEBUFFER, 0);
146 glDeleteFramebuffers(1, &fbo);
148 glDeleteTextures(1, &dst_texnum);
150 glDeleteTextures(1, &src_texnum);
153 resource_pool.release_glsl_program(glsl_program_num);
154 glDeleteVertexArrays(1, &vao);
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_2D, src_texnum);
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
207 glDisable(GL_DEPTH_TEST);
209 glDepthMask(GL_FALSE);
212 glViewport(0, 0, 512, 1);
214 vector<string> frag_shader_outputs;
215 GLuint glsl_program_num = resource_pool.compile_glsl_program(
216 read_version_dependent_file("vs", "vert"),
217 read_version_dependent_file("texture1d", "frag"),
218 frag_shader_outputs);
219 glUseProgram(glsl_program_num);
221 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
223 // Draw the texture stretched over a long quad, interpolating it out.
232 glGenVertexArrays(1, &vao);
234 glBindVertexArray(vao);
237 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
238 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same data.
240 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
243 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
244 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
249 // Now read the data back and see what the card did. (Ignore the last value.)
250 // (We only look at the red channel; the others will surely be the same.)
251 unsigned char out_data[512 * 4];
252 glReadPixels(0, 0, 512, 1, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
255 int wrongly_rounded = 0;
256 for (unsigned i = 0; i < 255; ++i) {
257 if (out_data[(i * 2 + 0) * 4] != i) {
260 if (out_data[(i * 2 + 1) * 4] != i + 1) {
265 movit_num_wrongly_rounded = wrongly_rounded;
268 glBindTexture(GL_TEXTURE_2D, 0);
270 glBindFramebuffer(GL_FRAMEBUFFER, 0);
272 glDeleteFramebuffers(1, &fbo);
274 glDeleteTextures(1, &dst_texnum);
276 glDeleteTextures(1, &src_texnum);
279 resource_pool.release_glsl_program(glsl_program_num);
280 glDeleteVertexArrays(1, &vao);
284 bool check_extensions()
286 // GLES generally doesn't use extensions as actively as desktop OpenGL.
287 // For now, we say that for GLES, we require GLES 3, which has everything
289 if (!epoxy_is_desktop_gl()) {
290 if (epoxy_gl_version() >= 30) {
293 fprintf(stderr, "Movit system requirements: GLES version %.1f is too old (GLES 3.0 needed).\n",
294 0.1f * epoxy_gl_version());
295 fprintf(stderr, "Movit initialization failed.\n");
300 if (epoxy_gl_version() < 30) {
301 fprintf(stderr, "Movit system requirements: OpenGL version %.1f is too old (OpenGL 3.0 needed).\n",
302 0.1f * epoxy_gl_version());
303 fprintf(stderr, "Movit initialization failed.\n");
307 // The user can specify that they want a timing report for each
308 // phase in an effect chain. However, that depends on this extension;
309 // without it, we do cannot even create the query objects.
310 movit_timer_queries_supported =
311 (epoxy_gl_version() >= 33 || epoxy_has_gl_extension("GL_ARB_timer_query"));
316 double get_glsl_version()
318 char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
320 // Skip past the first period.
321 char *ptr = strchr(glsl_version_str, '.');
325 // Now cut the string off at the next period or space, whatever comes first
326 // (unless the string ends first).
327 while (*ptr && *ptr != '.' && *ptr != ' ') {
332 // Now we have something on the form X.YY. We convert it to a float, and hope
333 // that if it's inexact (e.g. 1.30), atof() will round the same way the
335 std::istringstream locale_convert(glsl_version_str);
336 locale_convert.imbue(std::locale("C"));
338 locale_convert >> glsl_version;
339 free(glsl_version_str);
344 void APIENTRY debug_callback(GLenum source,
350 const void *userParam)
352 printf("Debug: %s\n", message);
357 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
359 if (movit_initialized) {
363 movit_data_directory = new string(data_directory);
364 movit_debug_level = debug_level;
367 glPixelStorei(GL_PACK_ALIGNMENT, 1);
368 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
369 glDisable(GL_DITHER);
371 // You can turn this on if you want detailed debug messages from the driver.
372 // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
373 // or you might not get much data back.
374 // glDebugMessageCallbackARB(callback, NULL);
375 // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
377 if (!check_extensions()) {
381 // Find out what shader model we should compile for.
382 // We need at least 1.30, due to use of (among others) integers.
383 if (epoxy_is_desktop_gl()) {
384 if (get_glsl_version() < 1.30f) {
385 fprintf(stderr, "Movit system requirements: Needs at least GLSL version 1.30 (has version %.1f)\n",
389 if (get_glsl_version() < 1.50f) {
390 movit_shader_model = MOVIT_GLSL_130;
392 // Note: All of our 1.50 shaders are identical to our 1.30 shaders,
393 // but OS X does not support 1.30; only 1.10 (which we don't support
394 // anymore) and 1.50 (and then only with core contexts). So we keep
395 // a second set of shaders around whose only difference is the different
396 // #version declaration.
397 movit_shader_model = MOVIT_GLSL_150;
400 movit_shader_model = MOVIT_ESSL_300;
403 measure_texel_subpixel_precision();
404 measure_roundoff_problems();
406 movit_initialized = true;