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()
31 if (texture_num != 0) {
32 glDeleteTextures(1, &texture_num);
37 void FlatInput::finalize()
39 // Translate the input format to OpenGL's enums.
40 GLenum internal_format;
41 if (type == GL_FLOAT) {
42 internal_format = GL_RGBA32F_ARB;
43 } else if (output_linear_gamma) {
44 assert(type == GL_UNSIGNED_BYTE);
45 internal_format = GL_SRGB8_ALPHA8;
47 assert(type == GL_UNSIGNED_BYTE);
48 internal_format = GL_RGBA8;
50 if (pixel_format == FORMAT_RGB) {
52 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
53 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
55 } else if (pixel_format == FORMAT_BGR) {
57 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
58 pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
60 } else if (pixel_format == FORMAT_GRAYSCALE) {
61 format = GL_LUMINANCE;
66 // Create the texture itself.
67 glGenTextures(1, &texture_num);
69 glBindTexture(GL_TEXTURE_2D, texture_num);
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
73 // Intel/Mesa seems to have a broken glGenerateMipmap() for non-FBO textures, so do it here
74 // instead of calling glGenerateMipmap().
75 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, needs_mipmaps ? GL_TRUE : GL_FALSE);
77 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
79 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
81 glBindTexture(GL_TEXTURE_2D, 0);
88 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
90 glActiveTexture(GL_TEXTURE0 + *sampler_num);
92 glBindTexture(GL_TEXTURE_2D, texture_num);
96 // Re-upload the texture.
97 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
99 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
101 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
103 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
105 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
111 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
114 needs_update = false;
117 // Bind it to a sampler.
118 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
122 std::string FlatInput::output_fragment_shader()
124 return read_file("flat_input.frag");