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_HALF_FLOAT || type == GL_UNSIGNED_SHORT || 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 if (pixel_format == FORMAT_R) {
50 internal_format = GL_R32F;
51 } else if (pixel_format == FORMAT_RG) {
52 internal_format = GL_RG32F;
54 internal_format = GL_RGBA32F;
56 } else if (type == GL_HALF_FLOAT) {
57 if (pixel_format == FORMAT_R) {
58 internal_format = GL_R16F;
59 } else if (pixel_format == FORMAT_RG) {
60 internal_format = GL_RG16F;
62 internal_format = GL_RGBA16F;
64 } else if (type == GL_UNSIGNED_SHORT) {
65 if (pixel_format == FORMAT_R) {
66 internal_format = GL_R16;
67 } else if (pixel_format == FORMAT_RG) {
68 internal_format = GL_RG16;
70 internal_format = GL_RGBA16;
72 } else if (output_linear_gamma) {
73 assert(type == GL_UNSIGNED_BYTE);
74 internal_format = GL_SRGB8_ALPHA8;
76 assert(type == GL_UNSIGNED_BYTE);
77 internal_format = GL_RGBA8;
79 if (pixel_format == FORMAT_RGB) {
81 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
82 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
84 } else if (pixel_format == FORMAT_BGR) {
86 } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
87 pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
89 } else if (pixel_format == FORMAT_GRAYSCALE) {
90 format = GL_LUMINANCE;
91 } else if (pixel_format == FORMAT_RG) {
97 // (Re-)upload the texture.
98 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
99 glBindTexture(GL_TEXTURE_2D, texture_num);
101 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
105 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
107 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
109 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
111 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
114 glGenerateMipmap(GL_TEXTURE_2D);
117 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
123 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
126 glBindTexture(GL_TEXTURE_2D, texture_num);
130 // Bind it to a sampler.
131 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
135 string FlatInput::output_fragment_shader()
137 return read_file("flat_input.frag");
140 void FlatInput::invalidate_pixel_data()
142 if (texture_num != 0) {
143 resource_pool->release_2d_texture(texture_num);