5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "resource_pool.h"
12 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
13 : image_format(image_format),
14 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::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
39 glActiveTexture(GL_TEXTURE0 + *sampler_num);
42 if (texture_num == 0) {
43 // Translate the input format to OpenGL's enums.
44 GLint internal_format;
46 if (type == GL_FLOAT) {
47 internal_format = GL_RGBA32F_ARB;
48 } else if (output_linear_gamma) {
49 assert(type == GL_UNSIGNED_BYTE);
50 internal_format = GL_SRGB8_ALPHA8;
52 assert(type == GL_UNSIGNED_BYTE);
53 internal_format = GL_RGBA8;
55 if (pixel_format == FORMAT_RGB) {
57 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
58 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
60 } else if (pixel_format == FORMAT_BGR) {
62 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
63 pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
65 } else if (pixel_format == FORMAT_GRAYSCALE) {
66 format = GL_LUMINANCE;
71 // (Re-)upload the texture.
72 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
73 glBindTexture(GL_TEXTURE_2D, texture_num);
75 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
79 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
81 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
83 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
85 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
88 glGenerateMipmap(GL_TEXTURE_2D);
91 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
97 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
100 glBindTexture(GL_TEXTURE_2D, texture_num);
104 // Bind it to a sampler.
105 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
109 string FlatInput::output_fragment_shader()
111 return read_file("flat_input.frag");
114 void FlatInput::invalidate_pixel_data()
116 if (texture_num != 0) {
117 resource_pool->release_2d_texture(texture_num);