]> git.sesse.net Git - movit/blob - init.cpp
Merge branch 'master' into epoxy
[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
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;
26
27 namespace {
28
29 void measure_texel_subpixel_precision()
30 {
31         ResourcePool resource_pool;
32         static const unsigned width = 4096;
33
34         // Generate a destination texture to render to, and an FBO.
35         GLuint dst_texnum, fbo;
36
37         glGenTextures(1, &dst_texnum);
38         check_error();
39         glBindTexture(GL_TEXTURE_2D, dst_texnum);
40         check_error();
41         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
42         check_error();
43
44         glGenFramebuffers(1, &fbo);
45         check_error();
46         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
47         check_error();
48         glFramebufferTexture2D(
49                 GL_FRAMEBUFFER,
50                 GL_COLOR_ATTACHMENT0,
51                 GL_TEXTURE_2D,
52                 dst_texnum,
53                 0);
54         check_error();
55
56         // Now generate a simple texture that's just [0,1].
57         GLuint src_texnum;
58         float texdata[] = { 0, 1 };
59         glGenTextures(1, &src_texnum);
60         check_error();
61         glBindTexture(GL_TEXTURE_2D, src_texnum);
62         check_error();
63         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64         check_error();
65         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
66         check_error();
67         glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, 2, 1, 0, GL_RED, GL_FLOAT, texdata);
68         check_error();
69
70         // Basic state.
71         glDisable(GL_BLEND);
72         check_error();
73         glDisable(GL_DEPTH_TEST);
74         check_error();
75         glDepthMask(GL_FALSE);
76         check_error();
77
78         glViewport(0, 0, width, 1);
79
80         GLuint glsl_program_num = resource_pool.compile_glsl_program(
81                 read_file("vs.vert"), read_file("texture1d.frag"));
82         glUseProgram(glsl_program_num);
83         check_error();
84         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
85         check_error();
86
87         // Draw the texture stretched over a long quad, interpolating it out.
88         // Note that since the texel center is in (0.5), we need to adjust the
89         // texture coordinates in order not to get long stretches of (1,1,1,...)
90         // at the start and (...,0,0,0) at the end.
91         float vertices[] = {
92                 0.0f, 1.0f,
93                 0.0f, 0.0f,
94                 1.0f, 1.0f,
95                 1.0f, 0.0f
96         };
97         float texcoords[] = {
98                 0.25f, 0.0f,
99                 0.25f, 0.0f,
100                 0.75f, 0.0f,
101                 0.75f, 0.0f
102         };
103
104         GLuint vao;
105         glGenVertexArrays(1, &vao);
106         check_error();
107         glBindVertexArray(vao);
108         check_error();
109
110         GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
111         GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(texcoords), texcoords);
112
113         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
114         check_error();
115
116         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
117         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
118
119         glUseProgram(0);
120         check_error();
121
122         // Now read the data back and see what the card did.
123         // (We only look at the red channel; the others will surely be the same.)
124         // We assume a linear ramp; anything else will give sort of odd results here.
125         float out_data[width];
126         glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
127         check_error();
128
129         float biggest_jump = 0.0f;
130         for (unsigned i = 1; i < width; ++i) {
131                 assert(out_data[i] >= out_data[i - 1]);
132                 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
133         }
134
135         assert(biggest_jump > 0.0);
136         movit_texel_subpixel_precision = biggest_jump;
137
138         // Clean up.
139         glBindTexture(GL_TEXTURE_2D, 0);
140         check_error();
141         glBindFramebuffer(GL_FRAMEBUFFER, 0);
142         check_error();
143         glDeleteFramebuffers(1, &fbo);
144         check_error();
145         glDeleteTextures(1, &dst_texnum);
146         check_error();
147         glDeleteTextures(1, &src_texnum);
148         check_error();
149
150         resource_pool.release_glsl_program(glsl_program_num);
151         glDeleteVertexArrays(1, &vao);
152         check_error();
153 }
154
155 void measure_roundoff_problems()
156 {
157         ResourcePool resource_pool;
158
159         // Generate a destination texture to render to, and an FBO.
160         GLuint dst_texnum, fbo;
161
162         glGenTextures(1, &dst_texnum);
163         check_error();
164         glBindTexture(GL_TEXTURE_2D, dst_texnum);
165         check_error();
166         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
167         check_error();
168
169         glGenFramebuffers(1, &fbo);
170         check_error();
171         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
172         check_error();
173         glFramebufferTexture2D(
174                 GL_FRAMEBUFFER,
175                 GL_COLOR_ATTACHMENT0,
176                 GL_TEXTURE_2D,
177                 dst_texnum,
178                 0);
179         check_error();
180
181         // Now generate a texture where every value except the last should be
182         // rounded up to the next one. However, there are cards (in highly
183         // common use) that can't do this right, for unknown reasons.
184         GLuint src_texnum;
185         float texdata[512];
186         for (int i = 0; i < 256; ++i) {
187                 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
188                 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
189         }
190         glGenTextures(1, &src_texnum);
191         check_error();
192         glBindTexture(GL_TEXTURE_2D, src_texnum);
193         check_error();
194         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
195         check_error();
196         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
197         check_error();
198         glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
199         check_error();
200
201         // Basic state.
202         glDisable(GL_BLEND);
203         check_error();
204         glDisable(GL_DEPTH_TEST);
205         check_error();
206         glDepthMask(GL_FALSE);
207         check_error();
208
209         glViewport(0, 0, 512, 1);
210
211         GLuint glsl_program_num = resource_pool.compile_glsl_program(
212                 read_file("vs.vert"), read_file("texture1d.frag"));
213         glUseProgram(glsl_program_num);
214         check_error();
215         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
216
217         // Draw the texture stretched over a long quad, interpolating it out.
218         float vertices[] = {
219                 0.0f, 1.0f,
220                 0.0f, 0.0f,
221                 1.0f, 1.0f,
222                 1.0f, 0.0f
223         };
224
225         GLuint vao;
226         glGenVertexArrays(1, &vao);
227         check_error();
228         glBindVertexArray(vao);
229         check_error();
230
231         GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
232         GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same data.
233
234         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
235         check_error();
236
237         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
238         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
239
240         glUseProgram(0);
241         check_error();
242
243         // Now read the data back and see what the card did. (Ignore the last value.)
244         // (We only look at the red channel; the others will surely be the same.)
245         unsigned char out_data[512];
246         glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
247         check_error();
248
249         int wrongly_rounded = 0;
250         for (unsigned i = 0; i < 255; ++i) {
251                 if (out_data[i * 2 + 0] != i) {
252                         ++wrongly_rounded;
253                 }
254                 if (out_data[i * 2 + 1] != i + 1) {
255                         ++wrongly_rounded;
256                 }
257         }
258
259         movit_num_wrongly_rounded = wrongly_rounded;
260
261         // Clean up.
262         glBindTexture(GL_TEXTURE_2D, 0);
263         check_error();
264         glBindFramebuffer(GL_FRAMEBUFFER, 0);
265         check_error();
266         glDeleteFramebuffers(1, &fbo);
267         check_error();
268         glDeleteTextures(1, &dst_texnum);
269         check_error();
270         glDeleteTextures(1, &src_texnum);
271         check_error();
272
273         resource_pool.release_glsl_program(glsl_program_num);
274         glDeleteVertexArrays(1, &vao);
275         check_error();
276 }
277
278 bool check_extensions()
279 {
280         // We fundamentally need FBOs and floating-point textures.
281         // FBOs are covered by OpenGL 1.5, and are not an extension there.
282         // Floating-point textures are part of OpenGL 3.0 and newer.
283         if (epoxy_gl_version() < 15 &&
284             !epoxy_has_gl_extension("GL_ARB_framebuffer_object")) return false;
285         if (epoxy_gl_version() < 30 &&
286             !epoxy_has_gl_extension("GL_ARB_texture_float")) return false;
287
288         // We assume that we can use non-power-of-two textures without restrictions.
289         if (epoxy_gl_version() < 20 &&
290             !epoxy_has_gl_extension("GL_ARB_texture_non_power_of_two")) return false;
291
292         // We also need GLSL fragment shaders.
293         if (epoxy_gl_version() < 20) {
294                 if (!epoxy_has_gl_extension("GL_ARB_fragment_shader")) return false;
295                 if (!epoxy_has_gl_extension("GL_ARB_shading_language_100")) return false;
296         }
297
298         // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
299         // but no modern card would really not provide it.)
300         if (epoxy_gl_version() < 21 &&
301             !epoxy_has_gl_extension("GL_ARB_pixel_buffer_object")) return false;
302
303         // ResampleEffect uses RG textures to encode a two-component LUT.
304         if (epoxy_gl_version() < 30 &&
305             !epoxy_has_gl_extension("GL_ARB_texture_rg")) return false;
306
307         // sRGB texture decode would be nice, but are not mandatory
308         // (GammaExpansionEffect can do the same thing if needed).
309         movit_srgb_textures_supported =
310                 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
311
312         // We may want to use round() at the end of the final shader,
313         // if supported. We need either GLSL 1.30 or this extension to do that,
314         // and 1.30 brings with it other things that we don't want to demand
315         // for now.
316         movit_shader_rounding_supported =
317                 (epoxy_gl_version() >= 30 || epoxy_has_gl_extension("GL_EXT_gpu_shader4"));
318
319         return true;
320 }
321
322 }  // namespace
323
324 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
325 {
326         if (movit_initialized) {
327                 return true;
328         }
329
330         movit_data_directory = new string(data_directory);
331         movit_debug_level = debug_level;
332
333         // geez 
334         glPixelStorei(GL_PACK_ALIGNMENT, 1);
335         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
336         glDisable(GL_DITHER);
337
338         if (!check_extensions()) {
339                 return false;
340         }
341         measure_texel_subpixel_precision();
342         measure_roundoff_problems();
343
344         movit_initialized = true;
345         return true;
346 }
347
348 }  // namespace movit