]> git.sesse.net Git - movit/blob - init.cpp
There's no need to #undef PREFIX, since we do the token pasting ourselves.
[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_timer_queries_supported;
19 int movit_num_wrongly_rounded;
20 MovitShaderModel movit_shader_model;
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, width, 1, 0, GL_RGBA, GL_FLOAT, 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         vector<string> frag_shader_outputs;
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                 frag_shader_outputs);
85         glUseProgram(glsl_program_num);
86         check_error();
87         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
88         check_error();
89
90         // Draw the texture stretched over a long quad, interpolating it out.
91         // Note that since the texel center is in (0.5), we need to adjust the
92         // texture coordinates in order not to get long stretches of (1,1,1,...)
93         // at the start and (...,0,0,0) at the end.
94         float vertices[] = {
95                 0.0f, 1.0f,
96                 0.0f, 0.0f,
97                 1.0f, 1.0f,
98                 1.0f, 0.0f
99         };
100         float texcoords[] = {
101                 0.25f, 0.0f,
102                 0.25f, 0.0f,
103                 0.75f, 0.0f,
104                 0.75f, 0.0f
105         };
106
107         GLuint vao;
108         glGenVertexArrays(1, &vao);
109         check_error();
110         glBindVertexArray(vao);
111         check_error();
112
113         GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
114         GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(texcoords), texcoords);
115
116         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
117         check_error();
118
119         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
120         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
121
122         glUseProgram(0);
123         check_error();
124
125         // Now read the data back and see what the card did.
126         // (We only look at the red channel; the others will surely be the same.)
127         // We assume a linear ramp; anything else will give sort of odd results here.
128         float out_data[width * 4];
129         glReadPixels(0, 0, width, 1, GL_RGBA, GL_FLOAT, out_data);
130         check_error();
131
132         float biggest_jump = 0.0f;
133         for (unsigned i = 1; i < width; ++i) {
134                 assert(out_data[i * 4] >= out_data[(i - 1) * 4]);
135                 biggest_jump = max(biggest_jump, out_data[i * 4] - out_data[(i - 1) * 4]);
136         }
137
138         assert(biggest_jump > 0.0);
139         movit_texel_subpixel_precision = biggest_jump;
140
141         // Clean up.
142         glBindTexture(GL_TEXTURE_2D, 0);
143         check_error();
144         glBindFramebuffer(GL_FRAMEBUFFER, 0);
145         check_error();
146         glDeleteFramebuffers(1, &fbo);
147         check_error();
148         glDeleteTextures(1, &dst_texnum);
149         check_error();
150         glDeleteTextures(1, &src_texnum);
151         check_error();
152
153         resource_pool.release_glsl_program(glsl_program_num);
154         glDeleteVertexArrays(1, &vao);
155         check_error();
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_2D, src_texnum);
196         check_error();
197         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198         check_error();
199         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
200         check_error();
201         glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, 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         vector<string> frag_shader_outputs;
215         GLuint glsl_program_num = resource_pool.compile_glsl_program(
216                 read_version_dependent_file("vs", "vert"),
217                 read_version_dependent_file("texture1d", "frag"),
218                 frag_shader_outputs);
219         glUseProgram(glsl_program_num);
220         check_error();
221         glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0);  // Bind the 2D sampler.
222
223         // Draw the texture stretched over a long quad, interpolating it out.
224         float vertices[] = {
225                 0.0f, 1.0f,
226                 0.0f, 0.0f,
227                 1.0f, 1.0f,
228                 1.0f, 0.0f
229         };
230
231         GLuint vao;
232         glGenVertexArrays(1, &vao);
233         check_error();
234         glBindVertexArray(vao);
235         check_error();
236
237         GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
238         GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same data.
239
240         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
241         check_error();
242
243         cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
244         cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
245
246         glUseProgram(0);
247         check_error();
248
249         // Now read the data back and see what the card did. (Ignore the last value.)
250         // (We only look at the red channel; the others will surely be the same.)
251         unsigned char out_data[512 * 4];
252         glReadPixels(0, 0, 512, 1, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
253         check_error();
254
255         int wrongly_rounded = 0;
256         for (unsigned i = 0; i < 255; ++i) {
257                 if (out_data[(i * 2 + 0) * 4] != i) {
258                         ++wrongly_rounded;
259                 }
260                 if (out_data[(i * 2 + 1) * 4] != i + 1) {
261                         ++wrongly_rounded;
262                 }
263         }
264
265         movit_num_wrongly_rounded = wrongly_rounded;
266
267         // Clean up.
268         glBindTexture(GL_TEXTURE_2D, 0);
269         check_error();
270         glBindFramebuffer(GL_FRAMEBUFFER, 0);
271         check_error();
272         glDeleteFramebuffers(1, &fbo);
273         check_error();
274         glDeleteTextures(1, &dst_texnum);
275         check_error();
276         glDeleteTextures(1, &src_texnum);
277         check_error();
278
279         resource_pool.release_glsl_program(glsl_program_num);
280         glDeleteVertexArrays(1, &vao);
281         check_error();
282 }
283
284 bool check_extensions()
285 {
286         // GLES generally doesn't use extensions as actively as desktop OpenGL.
287         // For now, we say that for GLES, we require GLES 3, which has everything
288         // we need.
289         if (!epoxy_is_desktop_gl()) {
290                 if (epoxy_gl_version() >= 30) {
291                         return true;
292                 } else {
293                         fprintf(stderr, "Movit system requirements: GLES version %.1f is too old (GLES 3.0 needed).\n",
294                                 0.1f * epoxy_gl_version());
295                         fprintf(stderr, "Movit initialization failed.\n");
296                         return false;
297                 }
298         }
299
300         if (epoxy_gl_version() < 30) {
301                 fprintf(stderr, "Movit system requirements: OpenGL version %.1f is too old (OpenGL 3.0 needed).\n",
302                         0.1f * epoxy_gl_version());
303                 fprintf(stderr, "Movit initialization failed.\n");
304                 return false;
305         }
306
307         // The user can specify that they want a timing report for each
308         // phase in an effect chain. However, that depends on this extension;
309         // without it, we do cannot even create the query objects.
310         movit_timer_queries_supported =
311                 (epoxy_gl_version() >= 33 || epoxy_has_gl_extension("GL_ARB_timer_query"));
312
313         return true;
314 }
315
316 double get_glsl_version()
317 {
318         char *glsl_version_str = strdup((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
319
320         // Skip past the first period.
321         char *ptr = strchr(glsl_version_str, '.');
322         assert(ptr != NULL);
323         ++ptr;
324
325         // Now cut the string off at the next period or space, whatever comes first
326         // (unless the string ends first).
327         while (*ptr && *ptr != '.' && *ptr != ' ') {
328                 ++ptr;
329         }
330         *ptr = '\0';
331
332         // Now we have something on the form X.YY. We convert it to a float, and hope
333         // that if it's inexact (e.g. 1.30), atof() will round the same way the
334         // compiler will.
335         std::istringstream locale_convert(glsl_version_str);
336         locale_convert.imbue(std::locale("C"));
337         double glsl_version;
338         locale_convert >> glsl_version;
339         free(glsl_version_str);
340
341         return glsl_version;
342 }
343
344 void APIENTRY debug_callback(GLenum source,
345                              GLenum type,
346                              GLuint id,
347                              GLenum severity,
348                              GLsizei length,
349                              const char *message,
350                              const void *userParam)
351 {
352         printf("Debug: %s\n", message);
353 }
354
355 }  // namespace
356
357 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
358 {
359         if (movit_initialized) {
360                 return true;
361         }
362
363         movit_data_directory = new string(data_directory);
364         movit_debug_level = debug_level;
365
366         // geez 
367         glPixelStorei(GL_PACK_ALIGNMENT, 1);
368         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
369         glDisable(GL_DITHER);
370
371         // You can turn this on if you want detailed debug messages from the driver.
372         // You should probably also ask for a debug context (see gtest_sdl_main.cpp),
373         // or you might not get much data back.
374         // glDebugMessageCallbackARB(callback, NULL);
375         // glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
376
377         if (!check_extensions()) {
378                 return false;
379         }
380
381         // Find out what shader model we should compile for.
382         // We need at least 1.30, due to use of (among others) integers.
383         if (epoxy_is_desktop_gl()) {
384                 if (get_glsl_version() < 1.30f) {
385                         fprintf(stderr, "Movit system requirements: Needs at least GLSL version 1.30 (has version %.1f)\n",
386                                 get_glsl_version());
387                         return false;
388                 }
389                 if (get_glsl_version() < 1.50f) {
390                         movit_shader_model = MOVIT_GLSL_130;
391                 } else {
392                         // Note: All of our 1.50 shaders are identical to our 1.30 shaders,
393                         // but OS X does not support 1.30; only 1.10 (which we don't support
394                         // anymore) and 1.50 (and then only with core contexts). So we keep
395                         // a second set of shaders around whose only difference is the different
396                         // #version declaration.
397                         movit_shader_model = MOVIT_GLSL_150;
398                 }
399         } else {
400                 movit_shader_model = MOVIT_ESSL_300;
401         }
402
403         measure_texel_subpixel_precision();
404         measure_roundoff_problems();
405
406         movit_initialized = true;
407         return true;
408 }
409
410 }  // namespace movit