]> git.sesse.net Git - movit/blob - init.cpp
Rewrite extension checking.
[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 int movit_num_wrongly_rounded;
20 bool movit_shader_rounding_supported;
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         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);
85         check_error();
86         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
87         check_error();
88
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.
93         float vertices[] = {
94                 0.0f, 1.0f,
95                 0.0f, 0.0f,
96                 1.0f, 1.0f,
97                 1.0f, 0.0f
98         };
99         float texcoords[] = {
100                 0.25f, 0.0f,
101                 0.25f, 0.0f,
102                 0.75f, 0.0f,
103                 0.75f, 0.0f
104         };
105
106         GLuint vao;
107         glGenVertexArrays(1, &vao);
108         check_error();
109         glBindVertexArray(vao);
110         check_error();
111
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);
114
115         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
116         check_error();
117
118         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
119         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
120
121         glUseProgram(0);
122         check_error();
123
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);
129         check_error();
130
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]);
135         }
136
137         assert(biggest_jump > 0.0);
138         movit_texel_subpixel_precision = biggest_jump;
139
140         // Clean up.
141         glBindTexture(GL_TEXTURE_2D, 0);
142         check_error();
143         glBindFramebuffer(GL_FRAMEBUFFER, 0);
144         check_error();
145         glDeleteFramebuffers(1, &fbo);
146         check_error();
147         glDeleteTextures(1, &dst_texnum);
148         check_error();
149         glDeleteTextures(1, &src_texnum);
150         check_error();
151
152         resource_pool.release_glsl_program(glsl_program_num);
153         glDeleteVertexArrays(1, &vao);
154         check_error();
155 }
156
157 void measure_roundoff_problems()
158 {
159         ResourcePool resource_pool;
160
161         // Generate a destination texture to render to, and an FBO.
162         GLuint dst_texnum, fbo;
163
164         glGenTextures(1, &dst_texnum);
165         check_error();
166         glBindTexture(GL_TEXTURE_2D, dst_texnum);
167         check_error();
168         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
169         check_error();
170
171         glGenFramebuffers(1, &fbo);
172         check_error();
173         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
174         check_error();
175         glFramebufferTexture2D(
176                 GL_FRAMEBUFFER,
177                 GL_COLOR_ATTACHMENT0,
178                 GL_TEXTURE_2D,
179                 dst_texnum,
180                 0);
181         check_error();
182
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.
186         GLuint src_texnum;
187         float texdata[512];
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;
191         }
192         glGenTextures(1, &src_texnum);
193         check_error();
194         glBindTexture(GL_TEXTURE_2D, src_texnum);
195         check_error();
196         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
197         check_error();
198         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
199         check_error();
200         glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
201         check_error();
202
203         // Basic state.
204         glDisable(GL_BLEND);
205         check_error();
206         glDisable(GL_DEPTH_TEST);
207         check_error();
208         glDepthMask(GL_FALSE);
209         check_error();
210
211         glViewport(0, 0, 512, 1);
212
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);
217         check_error();
218         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
219
220         // Draw the texture stretched over a long quad, interpolating it out.
221         float vertices[] = {
222                 0.0f, 1.0f,
223                 0.0f, 0.0f,
224                 1.0f, 1.0f,
225                 1.0f, 0.0f
226         };
227
228         GLuint vao;
229         glGenVertexArrays(1, &vao);
230         check_error();
231         glBindVertexArray(vao);
232         check_error();
233
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.
236
237         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
238         check_error();
239
240         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
241         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
242
243         glUseProgram(0);
244         check_error();
245
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);
250         check_error();
251
252         int wrongly_rounded = 0;
253         for (unsigned i = 0; i < 255; ++i) {
254                 if (out_data[(i * 2 + 0) * 4] != i) {
255                         ++wrongly_rounded;
256                 }
257                 if (out_data[(i * 2 + 1) * 4] != i + 1) {
258                         ++wrongly_rounded;
259                 }
260         }
261
262         movit_num_wrongly_rounded = wrongly_rounded;
263
264         // Clean up.
265         glBindTexture(GL_TEXTURE_2D, 0);
266         check_error();
267         glBindFramebuffer(GL_FRAMEBUFFER, 0);
268         check_error();
269         glDeleteFramebuffers(1, &fbo);
270         check_error();
271         glDeleteTextures(1, &dst_texnum);
272         check_error();
273         glDeleteTextures(1, &src_texnum);
274         check_error();
275
276         resource_pool.release_glsl_program(glsl_program_num);
277         glDeleteVertexArrays(1, &vao);
278         check_error();
279 }
280
281 struct RequiredExtension {
282         int min_equivalent_gl_version;
283         const char extension_name[64];
284 };
285 const RequiredExtension required_extensions[] = {
286         // We fundamentally need FBOs and floating-point textures.
287         // FBOs are covered by OpenGL 1.5, and are not an extension there.
288         // Floating-point textures are part of OpenGL 3.0 and newer.
289         { 15, "GL_ARB_framebuffer_object" },
290         { 30, "GL_ARB_texture_float" },
291
292         // We assume that we can use non-power-of-two textures without restrictions.
293         { 20, "GL_ARB_texture_non_power_of_two" },
294
295         // We also need GLSL fragment shaders.
296         { 20, "GL_ARB_fragment_shader" },
297         { 20, "GL_ARB_shading_language_100" },
298
299         // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
300         // but no modern card would really not provide it.)
301         { 21, "GL_ARB_pixel_buffer_object" },
302
303         // ResampleEffect uses RG textures to encode a two-component LUT.
304         // We also need GL_R several places, for single-channel input.
305         { 30, "GL_ARB_texture_rg" },
306 };
307
308 bool check_extensions()
309 {
310         // GLES generally doesn't use extensions as actively as desktop OpenGL.
311         // For now, we say that for GLES, we require GLES 3, which has everything
312         // we need.
313         if (!epoxy_is_desktop_gl()) {
314                 if (epoxy_gl_version() >= 30) {
315                         movit_srgb_textures_supported = true;
316                         movit_shader_rounding_supported = true;
317                         return true;
318                 } else {
319                         fprintf(stderr, "Movit system requirements: GLES version %.1f is too old (GLES 3.0 needed).\n",
320                                 0.1f * epoxy_gl_version());
321                         fprintf(stderr, "Movit initialization failed.\n");
322                         return false;
323                 }
324         }
325
326         // Check all extensions, and output errors for the ones that we are missing.
327         bool all_ok = true;
328         int gl_version = epoxy_gl_version();
329
330         for (unsigned i = 0; i < sizeof(required_extensions) / sizeof(required_extensions[0]); ++i) {
331                 if (gl_version < required_extensions[i].min_equivalent_gl_version &&
332                     !epoxy_has_gl_extension(required_extensions[i].extension_name)) {
333                         fprintf(stderr, "Movit system requirements: Needs extension '%s' or at least OpenGL version %.1f (has version %.1f)\n",
334                                 required_extensions[i].extension_name,
335                                 0.1f * required_extensions[i].min_equivalent_gl_version,
336                                 0.1f * gl_version);
337                         all_ok = false;
338                 }
339         }
340
341         if (!all_ok) {
342                 fprintf(stderr, "Movit initialization failed.\n");
343                 return false;
344         }
345
346         // sRGB texture decode would be nice, but are not mandatory
347         // (GammaExpansionEffect can do the same thing if needed).
348         movit_srgb_textures_supported =
349                 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
350
351         // We may want to use round() at the end of the final shader,
352         // if supported. We need either GLSL 1.30 or this extension to do that,
353         // and 1.30 brings with it other things that we don't want to demand
354         // for now.
355         movit_shader_rounding_supported =
356                 (epoxy_gl_version() >= 30 || epoxy_has_gl_extension("GL_EXT_gpu_shader4"));
357
358         return true;
359 }
360
361 double get_glsl_version()
362 {
363         char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
364
365         // Skip past the first period.
366         char *ptr = strchr(glsl_version_str, '.');
367         assert(ptr != NULL);
368         ++ptr;
369
370         // Now cut the string off at the next period or space, whatever comes first
371         // (unless the string ends first).
372         while (*ptr && *ptr != '.' && *ptr != ' ') {
373                 ++ptr;
374         }
375         *ptr = '\0';
376
377         // Now we have something on the form X.YY. We convert it to a float, and hope
378         // that if it's inexact (e.g. 1.30), atof() will round the same way the
379         // compiler will.
380         float glsl_version = atof(glsl_version_str);
381         free(glsl_version_str);
382
383         return glsl_version;
384 }
385
386 void APIENTRY debug_callback(GLenum source,
387                              GLenum type,
388                              GLuint id,
389                              GLenum severity,
390                              GLsizei length,
391                              const char *message,
392                              const void *userParam)
393 {
394         printf("Debug: %s\n", message);
395 }
396
397 }  // namespace
398
399 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
400 {
401         if (movit_initialized) {
402                 return true;
403         }
404
405         movit_data_directory = new string(data_directory);
406         movit_debug_level = debug_level;
407
408         // geez 
409         glPixelStorei(GL_PACK_ALIGNMENT, 1);
410         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
411         glDisable(GL_DITHER);
412
413         // You can turn this on if you want detailed debug messages from the driver.
414         // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
415         // or you might not get much data back.
416         // glDebugMessageCallbackARB(callback, NULL);
417         // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
418
419         if (!check_extensions()) {
420                 return false;
421         }
422
423         // Find out what shader model we should compile for.
424         if (epoxy_is_desktop_gl()) {
425                 if (get_glsl_version() >= 1.30) {
426                         movit_shader_model = MOVIT_GLSL_130;
427                 } else {
428                         movit_shader_model = MOVIT_GLSL_110;
429                 }
430         } else {
431                 movit_shader_model = MOVIT_ESSL_300;
432         }
433
434         measure_texel_subpixel_precision();
435         measure_roundoff_problems();
436
437         movit_initialized = true;
438         return true;
439 }
440
441 }  // namespace movit