]> git.sesse.net Git - movit/blob - init.cpp
a70ae058bbf39ddd0eb0b0ae2ee31b3cafef0be9
[movit] / init.cpp
1 #include <GL/glew.h>
2 #include <string>
3
4 #include "init.h"
5 #include "util.h"
6
7 bool movit_initialized = false;
8 float movit_texel_subpixel_precision;
9 bool movit_srgb_textures_supported;
10
11 // The rules for objects with nontrivial constructors in static scope
12 // are somewhat convoluted, and easy to mess up. We simply have a
13 // pointer instead (and never care to clean it up).
14 std::string *movit_data_directory = NULL;
15
16 namespace {
17
18 void measure_texel_subpixel_precision()
19 {
20         static const unsigned width = 1024;
21
22         // Generate a destination texture to render to, and an FBO.
23         GLuint dst_texnum, fbo;
24
25         glGenTextures(1, &dst_texnum);
26         check_error();
27         glBindTexture(GL_TEXTURE_2D, dst_texnum);
28         check_error();
29         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
30         check_error();
31
32         glGenFramebuffers(1, &fbo);
33         check_error();
34         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
35         check_error();
36         glFramebufferTexture2D(
37                 GL_FRAMEBUFFER,
38                 GL_COLOR_ATTACHMENT0,
39                 GL_TEXTURE_2D,
40                 dst_texnum,
41                 0);
42         check_error();
43
44         // Now generate a simple texture that's just [0,1].
45         GLuint src_texnum;
46         float texdata[] = { 0, 1 };
47         glGenTextures(1, &dst_texnum);
48         check_error();
49         glBindTexture(GL_TEXTURE_1D, dst_texnum);
50         check_error();
51         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
52         check_error();
53         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
54         check_error();
55         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 2, 0, GL_LUMINANCE, GL_FLOAT, texdata);
56         check_error();
57         glEnable(GL_TEXTURE_1D);
58         check_error();
59
60         // Basic state.
61         glDisable(GL_BLEND);
62         check_error();
63         glDisable(GL_DEPTH_TEST);
64         check_error();
65         glDepthMask(GL_FALSE);
66         check_error();
67
68         glViewport(0, 0, width, 1);
69
70         glMatrixMode(GL_PROJECTION);
71         glLoadIdentity();
72         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
73
74         glMatrixMode(GL_MODELVIEW);
75         glLoadIdentity();
76         check_error();
77
78         // Draw the texture stretched over a long quad, interpolating it out.
79         // Note that since the texel center is in (0.5), we need to adjust the
80         // texture coordinates in order not to get long stretches of (1,1,1,...)
81         // at the start and (...,0,0,0) at the end.
82         glBegin(GL_QUADS);
83
84         glTexCoord1f(0.25f);
85         glVertex2f(0.0f, 0.0f);
86
87         glTexCoord1f(0.75f);
88         glVertex2f(1.0f, 0.0f);
89
90         glTexCoord1f(0.75f);
91         glVertex2f(1.0f, 1.0f);
92
93         glTexCoord1f(0.25f);
94         glVertex2f(0.0f, 1.0f);
95
96         glEnd();
97         check_error();
98
99         glDisable(GL_TEXTURE_1D);
100         check_error();
101
102         // Now read the data back and see what the card did.
103         // (We only look at the red channel; the others will surely be the same.)
104         // We assume a linear ramp; anything else will give sort of odd results here.
105         float out_data[width];
106         glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
107         check_error();
108
109         float biggest_jump = 0.0f;
110         for (unsigned i = 1; i < width; ++i) {
111                 assert(out_data[i] >= out_data[i - 1]);
112                 biggest_jump = std::max(biggest_jump, out_data[i] - out_data[i - 1]);
113         }
114
115         movit_texel_subpixel_precision = biggest_jump;
116
117         // Clean up.
118         glBindTexture(GL_TEXTURE_1D, 0);
119         check_error();
120         glBindFramebuffer(GL_FRAMEBUFFER, 0);
121         check_error();
122         glDeleteFramebuffers(1, &fbo);
123         check_error();
124         glDeleteTextures(1, &dst_texnum);
125         check_error();
126         glDeleteTextures(1, &src_texnum);
127         check_error();
128 }
129
130 void check_extensions()
131 {
132         // We fundamentally need FBOs and floating-point textures.
133         assert(glewIsSupported("GL_ARB_framebuffer_object") != 0);
134         assert(glewIsSupported("GL_ARB_texture_float") != 0);
135
136         // We assume that we can use non-power-of-two textures without restrictions.
137         assert(glewIsSupported("GL_ARB_texture_non_power_of_two") != 0);
138
139         // We also need GLSL fragment shaders.
140         assert(glewIsSupported("GL_ARB_fragment_shader") != 0);
141         assert(glewIsSupported("GL_ARB_shading_language_100") != 0);
142
143         // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
144         // but no modern card would really not provide it.)
145         assert(glewIsSupported("GL_ARB_pixel_buffer_object") != 0);
146
147         // ResampleEffect uses RG textures to encode a two-component LUT.
148         assert(glewIsSupported("GL_ARB_texture_rg") != 0);
149
150         // sRGB texture decode would be nice, but are not mandatory
151         // (GammaExpansionEffect can do the same thing if needed).
152         movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
153 }
154
155 }  // namespace
156
157 void init_movit(const std::string& data_directory)
158 {
159         if (movit_initialized) {
160                 return;
161         }
162
163         movit_data_directory = new std::string(data_directory);
164
165         glewInit();
166
167         // geez 
168         glPixelStorei(GL_PACK_ALIGNMENT, 1);
169         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
170
171         measure_texel_subpixel_precision();
172         check_extensions();
173
174         movit_initialized = true;
175 }