5 #include "effect_util.h"
6 #include "flat_input.h"
9 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
10 : image_format(image_format),
11 pixel_format(pixel_format),
17 output_linear_gamma(false),
24 assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
25 register_int("output_linear_gamma", &output_linear_gamma);
26 register_int("needs_mipmaps", &needs_mipmaps);
29 FlatInput::~FlatInput()
32 glDeleteBuffers(1, &pbo);
35 if (texture_num != 0) {
36 glDeleteTextures(1, &texture_num);
41 void FlatInput::finalize()
43 // Translate the input format to OpenGL's enums.
44 GLenum internal_format;
45 if (type == GL_FLOAT) {
46 internal_format = GL_RGBA16F_ARB;
47 } else if (output_linear_gamma) {
48 assert(type == GL_UNSIGNED_BYTE);
49 internal_format = GL_SRGB8_ALPHA8;
51 assert(type == GL_UNSIGNED_BYTE);
52 internal_format = GL_RGBA8;
54 if (pixel_format == FORMAT_RGB) {
57 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
58 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
61 } else if (pixel_format == FORMAT_BGR) {
64 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
65 pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
68 } else if (pixel_format == FORMAT_GRAYSCALE) {
69 format = GL_LUMINANCE;
74 if (type == GL_FLOAT) {
75 bytes_per_pixel *= sizeof(float);
78 // Create PBO to hold the texture holding the input image, and then the texture itself.
79 glGenBuffers(1, &pbo);
81 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
83 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, pitch * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
85 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
88 glGenTextures(1, &texture_num);
90 glBindTexture(GL_TEXTURE_2D, texture_num);
92 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
96 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
98 // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
99 // instead of calling glGenerateMipmap().
100 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps ? GL_TRUE : GL_FALSE);
102 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
104 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
111 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
113 glActiveTexture(GL_TEXTURE0 + *sampler_num);
115 glBindTexture(GL_TEXTURE_2D, texture_num);
119 // Copy the pixel data into the PBO.
120 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
122 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
123 memcpy(mapped_pbo, pixel_data, pitch * height * bytes_per_pixel);
124 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
127 // Re-upload the texture from the PBO.
128 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
130 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, BUFFER_OFFSET(0));
132 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
138 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
141 needs_update = false;
144 // Bind it to a sampler.
145 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
149 std::string FlatInput::output_fragment_shader()
151 return read_file("flat_input.frag");