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