]> git.sesse.net Git - movit/blob - init.cpp
Release Movit 1.3.2. (From a branch, since I do not want to break ABI compatibility...
[movit] / init.cpp
1 #include <epoxy/gl.h>
2 #include <assert.h>
3 #include <stddef.h>
4 #include <algorithm>
5 #include <string>
6
7 #include "init.h"
8 #include "resource_pool.h"
9 #include "util.h"
10
11 using namespace std;
12
13 namespace movit {
14
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;
22
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;
27
28 namespace {
29
30 void measure_texel_subpixel_precision()
31 {
32         ResourcePool resource_pool;
33         static const unsigned width = 4096;
34
35         // Generate a destination texture to render to, and an FBO.
36         GLuint dst_texnum, fbo;
37
38         glGenTextures(1, &dst_texnum);
39         check_error();
40         glBindTexture(GL_TEXTURE_2D, dst_texnum);
41         check_error();
42         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, 1, 0, GL_RGBA, GL_FLOAT, NULL);
43         check_error();
44
45         glGenFramebuffers(1, &fbo);
46         check_error();
47         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
48         check_error();
49         glFramebufferTexture2D(
50                 GL_FRAMEBUFFER,
51                 GL_COLOR_ATTACHMENT0,
52                 GL_TEXTURE_2D,
53                 dst_texnum,
54                 0);
55         check_error();
56
57         // Now generate a simple texture that's just [0,1].
58         GLuint src_texnum;
59         float texdata[] = { 0, 1 };
60         glGenTextures(1, &src_texnum);
61         check_error();
62         glBindTexture(GL_TEXTURE_2D, src_texnum);
63         check_error();
64         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65         check_error();
66         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
67         check_error();
68         glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, 2, 1, 0, GL_RED, GL_FLOAT, texdata);
69         check_error();
70
71         // Basic state.
72         glDisable(GL_BLEND);
73         check_error();
74         glDisable(GL_DEPTH_TEST);
75         check_error();
76         glDepthMask(GL_FALSE);
77         check_error();
78
79         glViewport(0, 0, width, 1);
80
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"),
85                 frag_shader_outputs);
86         glUseProgram(glsl_program_num);
87         check_error();
88         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
89         check_error();
90
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.
95         float vertices[] = {
96                 0.0f, 1.0f,
97                 0.0f, 0.0f,
98                 1.0f, 1.0f,
99                 1.0f, 0.0f
100         };
101         float texcoords[] = {
102                 0.25f, 0.0f,
103                 0.25f, 0.0f,
104                 0.75f, 0.0f,
105                 0.75f, 0.0f
106         };
107
108         GLuint vao;
109         glGenVertexArrays(1, &vao);
110         check_error();
111         glBindVertexArray(vao);
112         check_error();
113
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);
116
117         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
118         check_error();
119
120         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
121         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
122
123         glUseProgram(0);
124         check_error();
125
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);
131         check_error();
132
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]);
137         }
138
139         assert(biggest_jump > 0.0);
140         movit_texel_subpixel_precision = biggest_jump;
141
142         // Clean up.
143         glBindTexture(GL_TEXTURE_2D, 0);
144         check_error();
145         glBindFramebuffer(GL_FRAMEBUFFER, 0);
146         check_error();
147         glDeleteFramebuffers(1, &fbo);
148         check_error();
149         glDeleteTextures(1, &dst_texnum);
150         check_error();
151         glDeleteTextures(1, &src_texnum);
152         check_error();
153
154         resource_pool.release_glsl_program(glsl_program_num);
155         glDeleteVertexArrays(1, &vao);
156         check_error();
157 }
158
159 void measure_roundoff_problems()
160 {
161         ResourcePool resource_pool;
162
163         // Generate a destination texture to render to, and an FBO.
164         GLuint dst_texnum, fbo;
165
166         glGenTextures(1, &dst_texnum);
167         check_error();
168         glBindTexture(GL_TEXTURE_2D, dst_texnum);
169         check_error();
170         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
171         check_error();
172
173         glGenFramebuffers(1, &fbo);
174         check_error();
175         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
176         check_error();
177         glFramebufferTexture2D(
178                 GL_FRAMEBUFFER,
179                 GL_COLOR_ATTACHMENT0,
180                 GL_TEXTURE_2D,
181                 dst_texnum,
182                 0);
183         check_error();
184
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.
188         GLuint src_texnum;
189         float texdata[512];
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;
193         }
194         glGenTextures(1, &src_texnum);
195         check_error();
196         glBindTexture(GL_TEXTURE_2D, src_texnum);
197         check_error();
198         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
199         check_error();
200         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
201         check_error();
202         glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
203         check_error();
204
205         // Basic state.
206         glDisable(GL_BLEND);
207         check_error();
208         glDisable(GL_DEPTH_TEST);
209         check_error();
210         glDepthMask(GL_FALSE);
211         check_error();
212
213         glViewport(0, 0, 512, 1);
214
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);
221         check_error();
222         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
223
224         // Draw the texture stretched over a long quad, interpolating it out.
225         float vertices[] = {
226                 0.0f, 1.0f,
227                 0.0f, 0.0f,
228                 1.0f, 1.0f,
229                 1.0f, 0.0f
230         };
231
232         GLuint vao;
233         glGenVertexArrays(1, &vao);
234         check_error();
235         glBindVertexArray(vao);
236         check_error();
237
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.
240
241         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
242         check_error();
243
244         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
245         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
246
247         glUseProgram(0);
248         check_error();
249
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);
254         check_error();
255
256         int wrongly_rounded = 0;
257         for (unsigned i = 0; i < 255; ++i) {
258                 if (out_data[(i * 2 + 0) * 4] != i) {
259                         ++wrongly_rounded;
260                 }
261                 if (out_data[(i * 2 + 1) * 4] != i + 1) {
262                         ++wrongly_rounded;
263                 }
264         }
265
266         movit_num_wrongly_rounded = wrongly_rounded;
267
268         // Clean up.
269         glBindTexture(GL_TEXTURE_2D, 0);
270         check_error();
271         glBindFramebuffer(GL_FRAMEBUFFER, 0);
272         check_error();
273         glDeleteFramebuffers(1, &fbo);
274         check_error();
275         glDeleteTextures(1, &dst_texnum);
276         check_error();
277         glDeleteTextures(1, &src_texnum);
278         check_error();
279
280         resource_pool.release_glsl_program(glsl_program_num);
281         glDeleteVertexArrays(1, &vao);
282         check_error();
283 }
284
285 struct RequiredExtension {
286         int min_equivalent_gl_version;
287         const char extension_name[64];
288 };
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" },
295
296         // We assume that we can use non-power-of-two textures without restrictions.
297         { 20, "GL_ARB_texture_non_power_of_two" },
298
299         // We also need GLSL fragment shaders.
300         { 20, "GL_ARB_fragment_shader" },
301         { 20, "GL_ARB_shading_language_100" },
302
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" },
306
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" },
310 };
311
312 bool check_extensions()
313 {
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
316         // we need.
317         if (!epoxy_is_desktop_gl()) {
318                 if (epoxy_gl_version() >= 30) {
319                         movit_srgb_textures_supported = true;
320                         return true;
321                 } else {
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");
325                         return false;
326                 }
327         }
328
329         // Check all extensions, and output errors for the ones that we are missing.
330         bool all_ok = true;
331         int gl_version = epoxy_gl_version();
332
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,
339                                 0.1f * gl_version);
340                         all_ok = false;
341                 }
342         }
343
344         if (!all_ok) {
345                 fprintf(stderr, "Movit initialization failed.\n");
346                 return false;
347         }
348
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"));
353
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"));
359
360         return true;
361 }
362
363 double get_glsl_version()
364 {
365         char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
366
367         // Skip past the first period.
368         char *ptr = strchr(glsl_version_str, '.');
369         assert(ptr != NULL);
370         ++ptr;
371
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 != ' ') {
375                 ++ptr;
376         }
377         *ptr = '\0';
378
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
381         // compiler will.
382         std::istringstream locale_convert(glsl_version_str);
383         locale_convert.imbue(std::locale("C"));
384         double glsl_version;
385         locale_convert >> glsl_version;
386         free(glsl_version_str);
387
388         return glsl_version;
389 }
390
391 void APIENTRY debug_callback(GLenum source,
392                              GLenum type,
393                              GLuint id,
394                              GLenum severity,
395                              GLsizei length,
396                              const char *message,
397                              const void *userParam)
398 {
399         printf("Debug: %s\n", message);
400 }
401
402 }  // namespace
403
404 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
405 {
406         if (movit_initialized) {
407                 return true;
408         }
409
410         movit_data_directory = new string(data_directory);
411         movit_debug_level = debug_level;
412
413         // geez 
414         glPixelStorei(GL_PACK_ALIGNMENT, 1);
415         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
416         glDisable(GL_DITHER);
417
418         // You can turn this on if you want detailed debug messages from the driver.
419         // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
420         // or you might not get much data back.
421         // glDebugMessageCallbackARB(callback, NULL);
422         // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
423
424         if (!check_extensions()) {
425                 return false;
426         }
427
428         // Find out what shader model we should compile for.
429         // We need at least 1.30, due to use of (among others) integers.
430         if (epoxy_is_desktop_gl()) {
431                 if (get_glsl_version() < 1.30f) {
432                         fprintf(stderr, "Movit system requirements: Needs at least GLSL version 1.30 (has version %.1f)\n",
433                                 get_glsl_version());
434                         return false;
435                 }
436                 if (get_glsl_version() < 1.50f) {
437                         movit_shader_model = MOVIT_GLSL_130;
438                 } else {
439                         // Note: All of our 1.50 shaders are identical to our 1.30 shaders,
440                         // but OS X does not support 1.30; only 1.10 (which we don't support
441                         // anymore) and 1.50 (and then only with core contexts). So we keep
442                         // a second set of shaders around whose only difference is the different
443                         // #version declaration.
444                         movit_shader_model = MOVIT_GLSL_150;
445                 }
446         } else {
447                 movit_shader_model = MOVIT_ESSL_300;
448         }
449
450         measure_texel_subpixel_precision();
451         measure_roundoff_problems();
452
453         movit_initialized = true;
454         return true;
455 }
456
457 }  // namespace movit