5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "resource_pool.h"
10 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
11 : image_format(image_format),
12 pixel_format(pixel_format),
18 output_linear_gamma(false),
25 assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
26 register_int("output_linear_gamma", &output_linear_gamma);
27 register_int("needs_mipmaps", &needs_mipmaps);
30 FlatInput::~FlatInput()
32 if (texture_num != 0) {
33 resource_pool->release_2d_texture(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 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
68 glBindTexture(GL_TEXTURE_2D, texture_num);
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
72 glBindTexture(GL_TEXTURE_2D, 0);
79 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
81 glActiveTexture(GL_TEXTURE0 + *sampler_num);
83 glBindTexture(GL_TEXTURE_2D, texture_num);
85 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
89 // Re-upload the texture.
90 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
92 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
96 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
99 glGenerateMipmap(GL_TEXTURE_2D);
102 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
108 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
111 needs_update = false;
114 // Bind it to a sampler.
115 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
119 std::string FlatInput::output_fragment_shader()
121 return read_file("flat_input.frag");