5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "resource_pool.h"
14 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
15 : image_format(image_format),
16 pixel_format(pixel_format),
20 output_linear_gamma(false),
27 assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
28 register_int("output_linear_gamma", &output_linear_gamma);
29 register_int("needs_mipmaps", &needs_mipmaps);
32 FlatInput::~FlatInput()
34 if (texture_num != 0) {
35 resource_pool->release_2d_texture(texture_num);
39 void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
41 glActiveTexture(GL_TEXTURE0 + *sampler_num);
44 if (texture_num == 0) {
45 // Translate the input format to OpenGL's enums.
46 GLint internal_format;
48 if (type == GL_FLOAT) {
49 internal_format = GL_RGBA32F_ARB;
50 } else if (output_linear_gamma) {
51 assert(type == GL_UNSIGNED_BYTE);
52 internal_format = GL_SRGB8_ALPHA8;
54 assert(type == GL_UNSIGNED_BYTE);
55 internal_format = GL_RGBA8;
57 if (pixel_format == FORMAT_RGB) {
59 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
60 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
62 } else if (pixel_format == FORMAT_BGR) {
64 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
65 pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
67 } else if (pixel_format == FORMAT_GRAYSCALE) {
68 format = GL_LUMINANCE;
73 // (Re-)upload the texture.
74 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
75 glBindTexture(GL_TEXTURE_2D, texture_num);
77 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
81 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
83 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
85 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
87 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
90 glGenerateMipmap(GL_TEXTURE_2D);
93 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
99 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
102 glBindTexture(GL_TEXTURE_2D, texture_num);
106 // Bind it to a sampler.
107 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
111 string FlatInput::output_fragment_shader()
113 return read_file("flat_input.frag");
116 void FlatInput::invalidate_pixel_data()
118 if (texture_num != 0) {
119 resource_pool->release_2d_texture(texture_num);