]> git.sesse.net Git - movit/blob - init.cpp
Explicitly declare use of round() as an #extension.
[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 "util.h"
9
10 using namespace std;
11
12 bool movit_initialized = false;
13 MovitDebugLevel movit_debug_level = MOVIT_DEBUG_ON;
14 float movit_texel_subpixel_precision;
15 bool movit_srgb_textures_supported;
16 int movit_num_wrongly_rounded;
17 bool movit_shader_rounding_supported;
18
19 // The rules for objects with nontrivial constructors in static scope
20 // are somewhat convoluted, and easy to mess up. We simply have a
21 // pointer instead (and never care to clean it up).
22 string *movit_data_directory = NULL;
23
24 namespace {
25
26 void measure_texel_subpixel_precision()
27 {
28         static const unsigned width = 4096;
29
30         // Generate a destination texture to render to, and an FBO.
31         GLuint dst_texnum, fbo;
32
33         glGenTextures(1, &dst_texnum);
34         check_error();
35         glBindTexture(GL_TEXTURE_2D, dst_texnum);
36         check_error();
37         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
38         check_error();
39
40         glGenFramebuffers(1, &fbo);
41         check_error();
42         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
43         check_error();
44         glFramebufferTexture2D(
45                 GL_FRAMEBUFFER,
46                 GL_COLOR_ATTACHMENT0,
47                 GL_TEXTURE_2D,
48                 dst_texnum,
49                 0);
50         check_error();
51
52         // Now generate a simple texture that's just [0,1].
53         GLuint src_texnum;
54         float texdata[] = { 0, 1 };
55         glGenTextures(1, &src_texnum);
56         check_error();
57         glBindTexture(GL_TEXTURE_1D, src_texnum);
58         check_error();
59         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60         check_error();
61         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
62         check_error();
63         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
64         check_error();
65         glEnable(GL_TEXTURE_1D);
66         check_error();
67
68         // Basic state.
69         glDisable(GL_BLEND);
70         check_error();
71         glDisable(GL_DEPTH_TEST);
72         check_error();
73         glDepthMask(GL_FALSE);
74         check_error();
75
76         glViewport(0, 0, width, 1);
77
78         glMatrixMode(GL_PROJECTION);
79         glLoadIdentity();
80         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
81
82         glMatrixMode(GL_MODELVIEW);
83         glLoadIdentity();
84         check_error();
85
86         // Draw the texture stretched over a long quad, interpolating it out.
87         // Note that since the texel center is in (0.5), we need to adjust the
88         // texture coordinates in order not to get long stretches of (1,1,1,...)
89         // at the start and (...,0,0,0) at the end.
90         glBegin(GL_QUADS);
91
92         glTexCoord1f(0.25f);
93         glVertex2f(0.0f, 0.0f);
94
95         glTexCoord1f(0.75f);
96         glVertex2f(1.0f, 0.0f);
97
98         glTexCoord1f(0.75f);
99         glVertex2f(1.0f, 1.0f);
100
101         glTexCoord1f(0.25f);
102         glVertex2f(0.0f, 1.0f);
103
104         glEnd();
105         check_error();
106
107         glDisable(GL_TEXTURE_1D);
108         check_error();
109
110         // Now read the data back and see what the card did.
111         // (We only look at the red channel; the others will surely be the same.)
112         // We assume a linear ramp; anything else will give sort of odd results here.
113         float out_data[width];
114         glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
115         check_error();
116
117         float biggest_jump = 0.0f;
118         for (unsigned i = 1; i < width; ++i) {
119                 assert(out_data[i] >= out_data[i - 1]);
120                 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
121         }
122
123         movit_texel_subpixel_precision = biggest_jump;
124
125         // Clean up.
126         glBindTexture(GL_TEXTURE_1D, 0);
127         check_error();
128         glBindFramebuffer(GL_FRAMEBUFFER, 0);
129         check_error();
130         glDeleteFramebuffers(1, &fbo);
131         check_error();
132         glDeleteTextures(1, &dst_texnum);
133         check_error();
134         glDeleteTextures(1, &src_texnum);
135         check_error();
136 }
137
138 void measure_roundoff_problems()
139 {
140         // Generate a destination texture to render to, and an FBO.
141         GLuint dst_texnum, fbo;
142
143         glGenTextures(1, &dst_texnum);
144         check_error();
145         glBindTexture(GL_TEXTURE_2D, dst_texnum);
146         check_error();
147         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
148         check_error();
149
150         glGenFramebuffers(1, &fbo);
151         check_error();
152         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
153         check_error();
154         glFramebufferTexture2D(
155                 GL_FRAMEBUFFER,
156                 GL_COLOR_ATTACHMENT0,
157                 GL_TEXTURE_2D,
158                 dst_texnum,
159                 0);
160         check_error();
161
162         // Now generate a texture where every value except the last should be
163         // rounded up to the next one. However, there are cards (in highly
164         // common use) that can't do this right, for unknown reasons.
165         GLuint src_texnum;
166         float texdata[512];
167         for (int i = 0; i < 256; ++i) {
168                 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
169                 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
170         }
171         glGenTextures(1, &src_texnum);
172         check_error();
173         glBindTexture(GL_TEXTURE_1D, src_texnum);
174         check_error();
175         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176         check_error();
177         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
178         check_error();
179         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE32F_ARB, 512, 0, GL_LUMINANCE, GL_FLOAT, texdata);
180         check_error();
181         glEnable(GL_TEXTURE_1D);
182         check_error();
183
184         // Basic state.
185         glDisable(GL_BLEND);
186         check_error();
187         glDisable(GL_DEPTH_TEST);
188         check_error();
189         glDepthMask(GL_FALSE);
190         check_error();
191
192         glViewport(0, 0, 512, 1);
193
194         glMatrixMode(GL_PROJECTION);
195         glLoadIdentity();
196         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
197
198         glMatrixMode(GL_MODELVIEW);
199         glLoadIdentity();
200         check_error();
201
202         // Draw the texture stretched over a long quad, interpolating it out.
203         glBegin(GL_QUADS);
204
205         glTexCoord1f(0.0f);
206         glVertex2f(0.0f, 0.0f);
207
208         glTexCoord1f(1.0f);
209         glVertex2f(1.0f, 0.0f);
210
211         glTexCoord1f(1.0f);
212         glVertex2f(1.0f, 1.0f);
213
214         glTexCoord1f(0.0f);
215         glVertex2f(0.0f, 1.0f);
216
217         glEnd();
218         check_error();
219
220         glDisable(GL_TEXTURE_1D);
221         check_error();
222
223         // Now read the data back and see what the card did. (Ignore the last value.)
224         // (We only look at the red channel; the others will surely be the same.)
225         unsigned char out_data[512];
226         glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
227         check_error();
228
229         int wrongly_rounded = 0;
230         for (unsigned i = 0; i < 255; ++i) {
231                 if (out_data[i * 2 + 0] != i) {
232                         ++wrongly_rounded;
233                 }
234                 if (out_data[i * 2 + 1] != i + 1) {
235                         ++wrongly_rounded;
236                 }
237         }
238
239         movit_num_wrongly_rounded = wrongly_rounded;
240
241         // Clean up.
242         glBindTexture(GL_TEXTURE_1D, 0);
243         check_error();
244         glBindFramebuffer(GL_FRAMEBUFFER, 0);
245         check_error();
246         glDeleteFramebuffers(1, &fbo);
247         check_error();
248         glDeleteTextures(1, &dst_texnum);
249         check_error();
250         glDeleteTextures(1, &src_texnum);
251         check_error();
252 }
253
254 void check_extensions()
255 {
256         // We fundamentally need FBOs and floating-point textures.
257         assert(glewIsSupported("GL_ARB_framebuffer_object") != 0);
258         assert(glewIsSupported("GL_ARB_texture_float") != 0);
259
260         // We assume that we can use non-power-of-two textures without restrictions.
261         assert(glewIsSupported("GL_ARB_texture_non_power_of_two") != 0);
262
263         // We also need GLSL fragment shaders.
264         assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
265         assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
266
267         // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
268         // but no modern card would really not provide it.)
269         assert(glewIsSupported("GL_ARB_pixel_buffer_object") != 0);
270
271         // ResampleEffect uses RG textures to encode a two-component LUT.
272         assert(glewIsSupported("GL_ARB_texture_rg") != 0);
273
274         // sRGB texture decode would be nice, but are not mandatory
275         // (GammaExpansionEffect can do the same thing if needed).
276         movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
277
278         // We may want to use round() at the end of the final shader,
279         // if supported. We need either GLSL 1.30 or this extension to do that,
280         // and 1.30 brings with it other things that we don't want to demand
281         // for now.
282         movit_shader_rounding_supported = glewIsSupported("GL_EXT_gpu_shader4");
283 }
284
285 }  // namespace
286
287 void init_movit(const string& data_directory, MovitDebugLevel debug_level)
288 {
289         if (movit_initialized) {
290                 return;
291         }
292
293         movit_data_directory = new string(data_directory);
294         movit_debug_level = debug_level;
295
296         glewInit();
297
298         // geez 
299         glPixelStorei(GL_PACK_ALIGNMENT, 1);
300         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
301         glDisable(GL_DITHER);
302
303         measure_texel_subpixel_precision();
304         measure_roundoff_problems();
305         check_extensions();
306
307         movit_initialized = true;
308 }