4 #include "flat_input.h"
8 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
9 : image_format(image_format),
10 pixel_format(pixel_format),
14 output_linear_gamma(false),
20 assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
21 register_int("output_linear_gamma", &output_linear_gamma);
22 register_int("needs_mipmaps", &needs_mipmaps);
25 void FlatInput::finalize()
27 // Translate the input format to OpenGL's enums.
28 GLenum internal_format;
29 if (type == GL_FLOAT) {
30 internal_format = GL_RGBA16F_ARB;
31 } else if (output_linear_gamma) {
32 assert(type == GL_UNSIGNED_BYTE);
33 internal_format = GL_SRGB8;
35 assert(type == GL_UNSIGNED_BYTE);
36 internal_format = GL_RGBA8;
38 if (pixel_format == FORMAT_RGB) {
41 } else if (pixel_format == FORMAT_RGBA) {
44 } else if (pixel_format == FORMAT_BGR) {
47 } else if (pixel_format == FORMAT_BGRA) {
50 } else if (pixel_format == FORMAT_GRAYSCALE) {
51 format = GL_LUMINANCE;
56 if (type == GL_FLOAT) {
57 bytes_per_pixel *= sizeof(float);
60 // Create PBO to hold the texture holding the input image, and then the texture itself.
61 glGenBuffers(1, &pbo);
63 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
65 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
67 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
70 glGenTextures(1, &texture_num);
72 glBindTexture(GL_TEXTURE_2D, texture_num);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
76 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
78 // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
79 // instead of calling glGenerateMipmap().
80 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps ? GL_TRUE : GL_FALSE);
82 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
84 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
91 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
93 glActiveTexture(GL_TEXTURE0 + *sampler_num);
95 glBindTexture(GL_TEXTURE_2D, texture_num);
99 // Copy the pixel data into the PBO.
100 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
102 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
103 memcpy(mapped_pbo, pixel_data, pitch * height * bytes_per_pixel);
104 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
107 // Re-upload the texture from the PBO.
108 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
110 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, BUFFER_OFFSET(0));
112 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
118 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
121 needs_update = false;
124 // Bind it to a sampler.
125 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
129 std::string FlatInput::output_fragment_shader()
131 return read_file("flat_input.frag");