]> git.sesse.net Git - movit/blob - init.cpp
4003163d9494843b3eccc42910ca41084b52a691
[movit] / init.cpp
1 #include <GL/glew.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_1D, src_texnum);
62         check_error();
63         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64         check_error();
65         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
66         check_error();
67         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, 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 1D 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, 0.0f,
93                 1.0f, 0.0f,
94                 1.0f, 1.0f,
95                 0.0f, 1.0f
96         };
97         float texcoords[] = {
98                 0.25f, 0.0f,
99                 0.75f, 0.0f,
100                 0.75f, 0.0f,
101                 0.25f, 0.0f
102         };
103
104         int position_attrib = glGetAttribLocation(glsl_program_num, "position");
105         assert(position_attrib != -1);
106         int texcoord_attrib = glGetAttribLocation(glsl_program_num, "texcoord");
107         assert(texcoord_attrib != -1);
108         glEnableVertexAttribArray(position_attrib);
109         check_error();
110         glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
111         check_error();
112         glEnableVertexAttribArray(texcoord_attrib);
113         check_error();
114         glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
115         check_error();
116
117         glDrawArrays(GL_QUADS, 0, 4);
118         check_error();
119
120         glUseProgram(0);
121         check_error();
122         glDisableVertexAttribArray(position_attrib);
123         check_error();
124         glDisableVertexAttribArray(texcoord_attrib);
125         check_error();
126
127         // Now read the data back and see what the card did.
128         // (We only look at the red channel; the others will surely be the same.)
129         // We assume a linear ramp; anything else will give sort of odd results here.
130         float out_data[width];
131         glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
132         check_error();
133
134         float biggest_jump = 0.0f;
135         for (unsigned i = 1; i < width; ++i) {
136                 assert(out_data[i] >= out_data[i - 1]);
137                 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
138         }
139
140         assert(biggest_jump > 0.0);
141         movit_texel_subpixel_precision = biggest_jump;
142
143         // Clean up.
144         glBindTexture(GL_TEXTURE_1D, 0);
145         check_error();
146         glBindFramebuffer(GL_FRAMEBUFFER, 0);
147         check_error();
148         glDeleteFramebuffers(1, &fbo);
149         check_error();
150         glDeleteTextures(1, &dst_texnum);
151         check_error();
152         glDeleteTextures(1, &src_texnum);
153         check_error();
154
155         resource_pool.release_glsl_program(glsl_program_num);
156 }
157
158 void measure_roundoff_problems()
159 {
160         ResourcePool resource_pool;
161
162         // Generate a destination texture to render to, and an FBO.
163         GLuint dst_texnum, fbo;
164
165         glGenTextures(1, &dst_texnum);
166         check_error();
167         glBindTexture(GL_TEXTURE_2D, dst_texnum);
168         check_error();
169         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
170         check_error();
171
172         glGenFramebuffers(1, &fbo);
173         check_error();
174         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
175         check_error();
176         glFramebufferTexture2D(
177                 GL_FRAMEBUFFER,
178                 GL_COLOR_ATTACHMENT0,
179                 GL_TEXTURE_2D,
180                 dst_texnum,
181                 0);
182         check_error();
183
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.
187         GLuint src_texnum;
188         float texdata[512];
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;
192         }
193         glGenTextures(1, &src_texnum);
194         check_error();
195         glBindTexture(GL_TEXTURE_1D, src_texnum);
196         check_error();
197         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198         check_error();
199         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
200         check_error();
201         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, 512, 0, GL_LUMINANCE, GL_FLOAT, texdata);
202         check_error();
203
204         // Basic state.
205         glDisable(GL_BLEND);
206         check_error();
207         glDisable(GL_DEPTH_TEST);
208         check_error();
209         glDepthMask(GL_FALSE);
210         check_error();
211
212         glViewport(0, 0, 512, 1);
213
214         GLuint glsl_program_num = resource_pool.compile_glsl_program(
215                 read_file("vs.vert"), read_file("texture1d.frag"));
216         glUseProgram(glsl_program_num);
217         check_error();
218         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 1D sampler.
219
220         // Draw the texture stretched over a long quad, interpolating it out.
221         float vertices[] = {
222                 0.0f, 0.0f,
223                 1.0f, 0.0f,
224                 1.0f, 1.0f,
225                 0.0f, 1.0f
226         };
227         float texcoords[] = {
228                 0.25f, 0.0f,
229                 0.75f, 0.0f,
230                 0.75f, 0.0f,
231                 0.25f, 0.0f
232         };
233         int position_attrib = glGetAttribLocation(glsl_program_num, "position");
234         assert(position_attrib != -1);
235         int texcoord_attrib = glGetAttribLocation(glsl_program_num, "texcoord");
236         assert(texcoord_attrib != -1);
237         glEnableVertexAttribArray(position_attrib);
238         check_error();
239         glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
240         check_error();
241         glEnableVertexAttribArray(texcoord_attrib);
242         check_error();
243         glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
244         check_error();
245         glDrawArrays(GL_QUADS, 0, 4);
246
247         glUseProgram(0);
248         check_error();
249         glDisableVertexAttribArray(position_attrib);
250         check_error();
251         glDisableVertexAttribArray(texcoord_attrib);
252
253         // Now read the data back and see what the card did. (Ignore the last value.)
254         // (We only look at the red channel; the others will surely be the same.)
255         unsigned char out_data[512];
256         glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
257         check_error();
258
259         int wrongly_rounded = 0;
260         for (unsigned i = 0; i < 255; ++i) {
261                 if (out_data[i * 2 + 0] != i) {
262                         ++wrongly_rounded;
263                 }
264                 if (out_data[i * 2 + 1] != i + 1) {
265                         ++wrongly_rounded;
266                 }
267         }
268
269         movit_num_wrongly_rounded = wrongly_rounded;
270
271         // Clean up.
272         glBindTexture(GL_TEXTURE_1D, 0);
273         check_error();
274         glBindFramebuffer(GL_FRAMEBUFFER, 0);
275         check_error();
276         glDeleteFramebuffers(1, &fbo);
277         check_error();
278         glDeleteTextures(1, &dst_texnum);
279         check_error();
280         glDeleteTextures(1, &src_texnum);
281         check_error();
282
283         resource_pool.release_glsl_program(glsl_program_num);
284 }
285
286 bool check_extensions()
287 {
288         // We fundamentally need FBOs and floating-point textures.
289         if (!glewIsSupported("GL_ARB_framebuffer_object")) return false;
290         if (!glewIsSupported("GL_ARB_texture_float")) return false;
291
292         // We assume that we can use non-power-of-two textures without restrictions.
293         if (!glewIsSupported("GL_ARB_texture_non_power_of_two")) return false;
294
295         // We also need GLSL fragment shaders.
296         if (!glewIsSupported("GL_ARB_fragment_shader")) return false;
297         if (!glewIsSupported("GL_ARB_shading_language_100")) return false;
298
299         // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
300         // but no modern card would really not provide it.)
301         if (!glewIsSupported("GL_ARB_pixel_buffer_object")) return false;
302
303         // ResampleEffect uses RG textures to encode a two-component LUT.
304         if (!glewIsSupported("GL_ARB_texture_rg")) return false;
305
306         // sRGB texture decode would be nice, but are not mandatory
307         // (GammaExpansionEffect can do the same thing if needed).
308         movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
309
310         // We may want to use round() at the end of the final shader,
311         // if supported. We need either GLSL 1.30 or this extension to do that,
312         // and 1.30 brings with it other things that we don't want to demand
313         // for now.
314         movit_shader_rounding_supported = glewIsSupported("GL_EXT_gpu_shader4");
315
316         return true;
317 }
318
319 }  // namespace
320
321 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
322 {
323         if (movit_initialized) {
324                 return true;
325         }
326
327         movit_data_directory = new string(data_directory);
328         movit_debug_level = debug_level;
329
330         GLenum err = glewInit();
331         if (err != GLEW_OK) {
332                 return false;
333         }
334
335         // geez 
336         glPixelStorei(GL_PACK_ALIGNMENT, 1);
337         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
338         glDisable(GL_DITHER);
339
340         if (!check_extensions()) {
341                 return false;
342         }
343         measure_texel_subpixel_precision();
344         measure_roundoff_problems();
345
346         movit_initialized = true;
347         return true;
348 }
349
350 }  // namespace movit