5 #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),
16 output_linear_gamma(false),
22 assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
23 register_int("output_linear_gamma", &output_linear_gamma);
24 register_int("needs_mipmaps", &needs_mipmaps);
27 FlatInput::~FlatInput()
30 glDeleteBuffers(1, &pbo);
33 if (texture_num != 0) {
34 glDeleteTextures(1, &texture_num);
39 void FlatInput::finalize()
41 // Translate the input format to OpenGL's enums.
42 GLenum internal_format;
43 if (type == GL_FLOAT) {
44 internal_format = GL_RGBA16F_ARB;
45 } else if (output_linear_gamma) {
46 assert(type == GL_UNSIGNED_BYTE);
47 internal_format = GL_SRGB8_ALPHA8;
49 assert(type == GL_UNSIGNED_BYTE);
50 internal_format = GL_RGBA8;
52 if (pixel_format == FORMAT_RGB) {
55 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
56 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
59 } else if (pixel_format == FORMAT_BGR) {
62 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
63 pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
66 } else if (pixel_format == FORMAT_GRAYSCALE) {
67 format = GL_LUMINANCE;
72 if (type == GL_FLOAT) {
73 bytes_per_pixel *= sizeof(float);
76 // Create PBO to hold the texture holding the input image, and then the texture itself.
77 glGenBuffers(1, &pbo);
79 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
81 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
83 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
86 glGenTextures(1, &texture_num);
88 glBindTexture(GL_TEXTURE_2D, texture_num);
90 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
94 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
96 // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
97 // instead of calling glGenerateMipmap().
98 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps ? GL_TRUE : GL_FALSE);
100 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
102 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
109 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
111 glActiveTexture(GL_TEXTURE0 + *sampler_num);
113 glBindTexture(GL_TEXTURE_2D, texture_num);
117 // Copy the pixel data into the PBO.
118 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
120 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
121 memcpy(mapped_pbo, pixel_data, pitch * height * bytes_per_pixel);
122 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
125 // Re-upload the texture from the PBO.
126 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
128 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, BUFFER_OFFSET(0));
130 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
136 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
139 needs_update = false;
142 // Bind it to a sampler.
143 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
147 std::string FlatInput::output_fragment_shader()
149 return read_file("flat_input.frag");