8 #include "resource_pool.h"
15 bool movit_initialized = false;
16 MovitDebugLevel movit_debug_level = MOVIT_DEBUG_ON;
17 float movit_texel_subpixel_precision;
18 bool movit_srgb_textures_supported;
19 int movit_num_wrongly_rounded;
20 bool movit_shader_rounding_supported;
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;
29 void measure_texel_subpixel_precision()
31 ResourcePool resource_pool;
32 static const unsigned width = 4096;
34 // Generate a destination texture to render to, and an FBO.
35 GLuint dst_texnum, fbo;
37 glGenTextures(1, &dst_texnum);
39 glBindTexture(GL_TEXTURE_2D, dst_texnum);
41 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, 1, 0, GL_RGBA, GL_FLOAT, NULL);
44 glGenFramebuffers(1, &fbo);
46 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
48 glFramebufferTexture2D(
56 // Now generate a simple texture that's just [0,1].
58 float texdata[] = { 0, 1 };
59 glGenTextures(1, &src_texnum);
61 glBindTexture(GL_TEXTURE_2D, src_texnum);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
67 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, 2, 1, 0, GL_RED, GL_FLOAT, texdata);
73 glDisable(GL_DEPTH_TEST);
75 glDepthMask(GL_FALSE);
78 glViewport(0, 0, width, 1);
80 GLuint glsl_program_num = resource_pool.compile_glsl_program(
81 read_file("vs.vert"), read_file("texture1d.frag"));
82 glUseProgram(glsl_program_num);
84 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
87 // Draw the texture stretched over a long quad, interpolating it out.
88 // Note that since the texel center is in (0.5), we need to adjust the
89 // texture coordinates in order not to get long stretches of (1,1,1,...)
90 // at the start and (...,0,0,0) at the end.
105 glGenVertexArrays(1, &vao);
107 glBindVertexArray(vao);
110 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
111 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(texcoords), texcoords);
113 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
116 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
117 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
122 // Now read the data back and see what the card did.
123 // (We only look at the red channel; the others will surely be the same.)
124 // We assume a linear ramp; anything else will give sort of odd results here.
125 float out_data[width];
126 glReadPixels(0, 0, width, 1, GL_RED, GL_FLOAT, out_data);
129 float biggest_jump = 0.0f;
130 for (unsigned i = 1; i < width; ++i) {
131 assert(out_data[i] >= out_data[i - 1]);
132 biggest_jump = max(biggest_jump, out_data[i] - out_data[i - 1]);
135 assert(biggest_jump > 0.0);
136 movit_texel_subpixel_precision = biggest_jump;
139 glBindTexture(GL_TEXTURE_2D, 0);
141 glBindFramebuffer(GL_FRAMEBUFFER, 0);
143 glDeleteFramebuffers(1, &fbo);
145 glDeleteTextures(1, &dst_texnum);
147 glDeleteTextures(1, &src_texnum);
150 resource_pool.release_glsl_program(glsl_program_num);
151 glDeleteVertexArrays(1, &vao);
155 void measure_roundoff_problems()
157 ResourcePool resource_pool;
159 // Generate a destination texture to render to, and an FBO.
160 GLuint dst_texnum, fbo;
162 glGenTextures(1, &dst_texnum);
164 glBindTexture(GL_TEXTURE_2D, dst_texnum);
166 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
169 glGenFramebuffers(1, &fbo);
171 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
173 glFramebufferTexture2D(
175 GL_COLOR_ATTACHMENT0,
181 // Now generate a texture where every value except the last should be
182 // rounded up to the next one. However, there are cards (in highly
183 // common use) that can't do this right, for unknown reasons.
186 for (int i = 0; i < 256; ++i) {
187 texdata[i * 2 + 0] = (i + 0.48) / 255.0;
188 texdata[i * 2 + 1] = (i + 0.52) / 255.0;
190 glGenTextures(1, &src_texnum);
192 glBindTexture(GL_TEXTURE_2D, src_texnum);
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
198 glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 1, 0, GL_RED, GL_FLOAT, texdata);
204 glDisable(GL_DEPTH_TEST);
206 glDepthMask(GL_FALSE);
209 glViewport(0, 0, 512, 1);
211 GLuint glsl_program_num = resource_pool.compile_glsl_program(
212 read_file("vs.vert"), read_file("texture1d.frag"));
213 glUseProgram(glsl_program_num);
215 glUniform1i(glGetUniformLocation(glsl_program_num, "tex"), 0); // Bind the 2D sampler.
217 // Draw the texture stretched over a long quad, interpolating it out.
226 glGenVertexArrays(1, &vao);
228 glBindVertexArray(vao);
231 GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
232 GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same data.
234 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
237 cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
238 cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
243 // Now read the data back and see what the card did. (Ignore the last value.)
244 // (We only look at the red channel; the others will surely be the same.)
245 unsigned char out_data[512];
246 glReadPixels(0, 0, 512, 1, GL_RED, GL_UNSIGNED_BYTE, out_data);
249 int wrongly_rounded = 0;
250 for (unsigned i = 0; i < 255; ++i) {
251 if (out_data[i * 2 + 0] != i) {
254 if (out_data[i * 2 + 1] != i + 1) {
259 movit_num_wrongly_rounded = wrongly_rounded;
262 glBindTexture(GL_TEXTURE_2D, 0);
264 glBindFramebuffer(GL_FRAMEBUFFER, 0);
266 glDeleteFramebuffers(1, &fbo);
268 glDeleteTextures(1, &dst_texnum);
270 glDeleteTextures(1, &src_texnum);
273 resource_pool.release_glsl_program(glsl_program_num);
274 glDeleteVertexArrays(1, &vao);
278 bool check_extensions()
280 // We fundamentally need FBOs and floating-point textures.
281 if (!glewIsSupported("GL_ARB_framebuffer_object")) return false;
282 if (!glewIsSupported("GL_ARB_texture_float")) return false;
284 // We assume that we can use non-power-of-two textures without restrictions.
285 if (!glewIsSupported("GL_ARB_texture_non_power_of_two")) return false;
287 // We also need GLSL fragment shaders.
288 if (!glewIsSupported("GL_ARB_fragment_shader")) return false;
289 if (!glewIsSupported("GL_ARB_shading_language_100")) return false;
291 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
292 // but no modern card would really not provide it.)
293 if (!glewIsSupported("GL_ARB_pixel_buffer_object")) return false;
295 // ResampleEffect uses RG textures to encode a two-component LUT.
296 if (!glewIsSupported("GL_ARB_texture_rg")) return false;
298 // sRGB texture decode would be nice, but are not mandatory
299 // (GammaExpansionEffect can do the same thing if needed).
300 movit_srgb_textures_supported = glewIsSupported("GL_EXT_texture_sRGB");
302 // We may want to use round() at the end of the final shader,
303 // if supported. We need either GLSL 1.30 or this extension to do that,
304 // and 1.30 brings with it other things that we don't want to demand
306 movit_shader_rounding_supported = glewIsSupported("GL_EXT_gpu_shader4");
313 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
315 if (movit_initialized) {
319 movit_data_directory = new string(data_directory);
320 movit_debug_level = debug_level;
322 GLenum err = glewInit();
323 if (err != GLEW_OK) {
328 glPixelStorei(GL_PACK_ALIGNMENT, 1);
329 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
330 glDisable(GL_DITHER);
332 if (!check_extensions()) {
335 measure_texel_subpixel_precision();
336 measure_roundoff_problems();
338 movit_initialized = true;