]> git.sesse.net Git - movit/blob - init.cpp
Merge branch 'epoxy' of ssh://pannekake.samfundet.no/srv/git.sesse.net/www/movit...
[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 bool check_extensions()
282 {
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
285         // we need.
286         if (!epoxy_is_desktop_gl()) {
287                 if (epoxy_gl_version() >= 30) {
288                         movit_srgb_textures_supported = true;
289                         movit_shader_rounding_supported = true;
290                 } else {
291                         return false;
292                 }
293         }
294
295         // We fundamentally need FBOs and floating-point textures.
296         // FBOs are covered by OpenGL 1.5, and are not an extension there.
297         // Floating-point textures are part of OpenGL 3.0 and newer.
298         if (epoxy_gl_version() < 15 &&
299             !epoxy_has_gl_extension("GL_ARB_framebuffer_object")) return false;
300         if (epoxy_gl_version() < 30 &&
301             !epoxy_has_gl_extension("GL_ARB_texture_float")) return false;
302
303         // We assume that we can use non-power-of-two textures without restrictions.
304         if (epoxy_gl_version() < 20 &&
305             !epoxy_has_gl_extension("GL_ARB_texture_non_power_of_two")) return false;
306
307         // We also need GLSL fragment shaders.
308         if (epoxy_gl_version() < 20) {
309                 if (!epoxy_has_gl_extension("GL_ARB_fragment_shader")) return false;
310                 if (!epoxy_has_gl_extension("GL_ARB_shading_language_100")) return false;
311         }
312
313         // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
314         // but no modern card would really not provide it.)
315         if (epoxy_gl_version() < 21 &&
316             !epoxy_has_gl_extension("GL_ARB_pixel_buffer_object")) return false;
317
318         // ResampleEffect uses RG textures to encode a two-component LUT.
319         if (epoxy_gl_version() < 30 &&
320             !epoxy_has_gl_extension("GL_ARB_texture_rg")) return false;
321
322         // sRGB texture decode would be nice, but are not mandatory
323         // (GammaExpansionEffect can do the same thing if needed).
324         movit_srgb_textures_supported =
325                 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
326
327         // We may want to use round() at the end of the final shader,
328         // if supported. We need either GLSL 1.30 or this extension to do that,
329         // and 1.30 brings with it other things that we don't want to demand
330         // for now.
331         movit_shader_rounding_supported =
332                 (epoxy_gl_version() >= 30 || epoxy_has_gl_extension("GL_EXT_gpu_shader4"));
333
334         return true;
335 }
336
337 double get_glsl_version()
338 {
339         char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
340
341         // Skip past the first period.
342         char *ptr = strchr(glsl_version_str, '.');
343         assert(ptr != NULL);
344         ++ptr;
345
346         // Now cut the string off at the next period or space, whatever comes first
347         // (unless the string ends first).
348         while (*ptr && *ptr != '.' && *ptr != ' ') {
349                 ++ptr;
350         }
351         *ptr = '\0';
352
353         // Now we have something on the form X.YY. We convert it to a float, and hope
354         // that if it's inexact (e.g. 1.30), atof() will round the same way the
355         // compiler will.
356         float glsl_version = atof(glsl_version_str);
357         free(glsl_version_str);
358
359         return glsl_version;
360 }
361
362 void APIENTRY debug_callback(GLenum source,
363                              GLenum type,
364                              GLuint id,
365                              GLenum severity,
366                              GLsizei length,
367                              const char *message,
368                              const void *userParam)
369 {
370         printf("Debug: %s\n", message);
371 }
372
373 }  // namespace
374
375 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
376 {
377         if (movit_initialized) {
378                 return true;
379         }
380
381         movit_data_directory = new string(data_directory);
382         movit_debug_level = debug_level;
383
384         // geez 
385         glPixelStorei(GL_PACK_ALIGNMENT, 1);
386         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
387         glDisable(GL_DITHER);
388
389         // You can turn this on if you want detailed debug messages from the driver.
390         // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
391         // or you might not get much data back.
392         // glDebugMessageCallbackARB(callback, NULL);
393         // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
394
395         if (!check_extensions()) {
396                 return false;
397         }
398
399         // Find out what shader model we should compile for.
400         if (epoxy_is_desktop_gl()) {
401                 if (get_glsl_version() >= 1.30) {
402                         movit_shader_model = MOVIT_GLSL_130;
403                 } else {
404                         movit_shader_model = MOVIT_GLSL_110;
405                 }
406         } else {
407                 movit_shader_model = MOVIT_ESSL_300;
408         }
409
410         measure_texel_subpixel_precision();
411         measure_roundoff_problems();
412
413         movit_initialized = true;
414         return true;
415 }
416
417 }  // namespace movit