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