5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "resource_pool.h"
14 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format_in, GLenum type, unsigned width, unsigned height)
15 : image_format(image_format),
19 output_linear_gamma(false),
26 fixup_red_to_grayscale(false)
28 assert(type == GL_FLOAT || type == GL_HALF_FLOAT || type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_BYTE);
29 register_int("output_linear_gamma", &output_linear_gamma);
30 register_int("needs_mipmaps", &needs_mipmaps);
31 register_uniform_sampler2d("tex", &uniform_tex);
33 // Some types are not supported in all GL versions (e.g. GLES),
34 // and will corrected into the right format in the shader.
35 switch (pixel_format_in) {
36 case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
37 pixel_format = FORMAT_RGBA_PREMULTIPLIED_ALPHA;
40 case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
41 pixel_format = FORMAT_RGBA_POSTMULTIPLIED_ALPHA;
45 pixel_format = FORMAT_RGB;
48 case FORMAT_GRAYSCALE:
49 pixel_format = FORMAT_R;
50 fixup_red_to_grayscale = true;
53 pixel_format = pixel_format_in;
58 FlatInput::~FlatInput()
60 if (texture_num != 0) {
61 resource_pool->release_2d_texture(texture_num);
65 void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
67 glActiveTexture(GL_TEXTURE0 + *sampler_num);
70 if (texture_num == 0) {
71 // Translate the input format to OpenGL's enums.
72 GLint internal_format;
74 if (type == GL_FLOAT) {
75 if (pixel_format == FORMAT_R) {
76 internal_format = GL_R32F;
77 } else if (pixel_format == FORMAT_RG) {
78 internal_format = GL_RG32F;
79 } else if (pixel_format == FORMAT_RGB) {
80 internal_format = GL_RGB32F;
82 internal_format = GL_RGBA32F;
84 } else if (type == GL_HALF_FLOAT) {
85 if (pixel_format == FORMAT_R) {
86 internal_format = GL_R16F;
87 } else if (pixel_format == FORMAT_RG) {
88 internal_format = GL_RG16F;
89 } else if (pixel_format == FORMAT_RGB) {
90 internal_format = GL_RGB16F;
92 internal_format = GL_RGBA16F;
94 } else if (type == GL_UNSIGNED_SHORT) {
95 if (pixel_format == FORMAT_R) {
96 internal_format = GL_R16;
97 } else if (pixel_format == FORMAT_RG) {
98 internal_format = GL_RG16;
99 } else if (pixel_format == FORMAT_RGB) {
100 internal_format = GL_RGB16;
102 internal_format = GL_RGBA16;
104 } else if (output_linear_gamma) {
105 assert(type == GL_UNSIGNED_BYTE);
106 if (pixel_format == FORMAT_RGB) {
107 internal_format = GL_SRGB8;
108 } else if (pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
109 internal_format = GL_SRGB8_ALPHA8;
114 assert(type == GL_UNSIGNED_BYTE);
115 if (pixel_format == FORMAT_R) {
116 internal_format = GL_R8;
117 } else if (pixel_format == FORMAT_RG) {
118 internal_format = GL_RG8;
119 } else if (pixel_format == FORMAT_RGB) {
120 internal_format = GL_RGB8;
122 internal_format = GL_RGBA8;
125 if (pixel_format == FORMAT_RGB) {
127 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
128 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
130 } else if (pixel_format == FORMAT_RG) {
132 } else if (pixel_format == FORMAT_R) {
138 // (Re-)upload the texture.
139 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
140 glBindTexture(GL_TEXTURE_2D, texture_num);
142 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
146 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
148 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
150 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
152 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
155 glGenerateMipmap(GL_TEXTURE_2D);
158 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
164 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
167 glBindTexture(GL_TEXTURE_2D, texture_num);
171 // Bind it to a sampler.
172 uniform_tex = *sampler_num;
176 string FlatInput::output_fragment_shader()
179 sprintf(buf, "#define FIXUP_SWAP_RB %d\n#define FIXUP_RED_TO_GRAYSCALE %d\n",
180 fixup_swap_rb, fixup_red_to_grayscale);
181 return buf + read_file("flat_input.frag");
184 void FlatInput::invalidate_pixel_data()
186 if (texture_num != 0) {
187 resource_pool->release_2d_texture(texture_num);