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 * 4];
126 glReadPixels(0, 0, width, 1, GL_RGBA, GL_FLOAT, out_data);
129 float biggest_jump = 0.0f;
130 for (unsigned i = 1; i < width; ++i) {
131 assert(out_data[i * 4] >= out_data[(i - 1) * 4]);
132 biggest_jump = max(biggest_jump, out_data[i * 4] - out_data[(i - 1) * 4]);
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 * 4];
246 glReadPixels(0, 0, 512, 1, GL_RGBA, 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) * 4] != i) {
254 if (out_data[(i * 2 + 1) * 4] != 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 // GLES generally doesn't use extensions as actively as desktop OpenGL.
281 // For now, we say that for GLES, we require GLES 3, which has everything
284 // Since we use implicit #version 100, we don't have round(). We will
285 // fix this at some later stage.
286 if (!epoxy_is_desktop_gl()) {
287 if (epoxy_gl_version() >= 30) {
288 movit_srgb_textures_supported = true;
289 movit_shader_rounding_supported = false;
295 // We fundamentally need FBOs and floating-point textures.
296 // FBOs are covered by OpenGL 1.5, and are not an extension there.
297 // Floating-point textures are part of OpenGL 3.0 and newer.
298 if (epoxy_gl_version() < 15 &&
299 !epoxy_has_gl_extension("GL_ARB_framebuffer_object")) return false;
300 if (epoxy_gl_version() < 30 &&
301 !epoxy_has_gl_extension("GL_ARB_texture_float")) return false;
303 // We assume that we can use non-power-of-two textures without restrictions.
304 if (epoxy_gl_version() < 20 &&
305 !epoxy_has_gl_extension("GL_ARB_texture_non_power_of_two")) return false;
307 // We also need GLSL fragment shaders.
308 if (epoxy_gl_version() < 20) {
309 if (!epoxy_has_gl_extension("GL_ARB_fragment_shader")) return false;
310 if (!epoxy_has_gl_extension("GL_ARB_shading_language_100")) return false;
313 // FlatInput and YCbCrInput uses PBOs. (They could in theory do without,
314 // but no modern card would really not provide it.)
315 if (epoxy_gl_version() < 21 &&
316 !epoxy_has_gl_extension("GL_ARB_pixel_buffer_object")) return false;
318 // ResampleEffect uses RG textures to encode a two-component LUT.
319 if (epoxy_gl_version() < 30 &&
320 !epoxy_has_gl_extension("GL_ARB_texture_rg")) return false;
322 // sRGB texture decode would be nice, but are not mandatory
323 // (GammaExpansionEffect can do the same thing if needed).
324 movit_srgb_textures_supported =
325 (epoxy_gl_version() >= 21 || epoxy_has_gl_extension("GL_EXT_texture_sRGB"));
327 // We may want to use round() at the end of the final shader,
328 // if supported. We need either GLSL 1.30 or this extension to do that,
329 // and 1.30 brings with it other things that we don't want to demand
331 movit_shader_rounding_supported =
332 (epoxy_gl_version() >= 30 || epoxy_has_gl_extension("GL_EXT_gpu_shader4"));
339 bool init_movit(const string& data_directory, MovitDebugLevel debug_level)
341 if (movit_initialized) {
345 movit_data_directory = new string(data_directory);
346 movit_debug_level = debug_level;
349 glPixelStorei(GL_PACK_ALIGNMENT, 1);
350 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
351 glDisable(GL_DITHER);
353 if (!check_extensions()) {
356 measure_texel_subpixel_precision();
357 measure_roundoff_problems();
359 movit_initialized = true;