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),
27 fixup_red_to_grayscale(false)
29 assert(type == GL_FLOAT || type == GL_HALF_FLOAT || type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_BYTE);
30 register_int("output_linear_gamma", &output_linear_gamma);
31 register_int("needs_mipmaps", &needs_mipmaps);
32 register_uniform_sampler2d("tex", &uniform_tex);
34 // Some types are not supported in all GL versions (e.g. GLES),
35 // and will corrected into the right format in the shader.
36 switch (pixel_format_in) {
37 case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
38 pixel_format = FORMAT_RGBA_PREMULTIPLIED_ALPHA;
41 case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
42 pixel_format = FORMAT_RGBA_POSTMULTIPLIED_ALPHA;
46 pixel_format = FORMAT_RGB;
49 case FORMAT_GRAYSCALE:
50 pixel_format = FORMAT_R;
51 fixup_red_to_grayscale = true;
54 pixel_format = pixel_format_in;
59 FlatInput::~FlatInput()
61 possibly_release_texture();
64 void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
66 glActiveTexture(GL_TEXTURE0 + *sampler_num);
69 if (texture_num == 0 && (pbo != 0 || pixel_data != nullptr)) {
70 // Translate the input format to OpenGL's enums.
71 GLint internal_format;
73 if (type == GL_FLOAT) {
74 if (pixel_format == FORMAT_R) {
75 internal_format = GL_R32F;
76 } else if (pixel_format == FORMAT_RG) {
77 internal_format = GL_RG32F;
78 } else if (pixel_format == FORMAT_RGB) {
79 internal_format = GL_RGB32F;
81 internal_format = GL_RGBA32F;
83 } else if (type == GL_HALF_FLOAT) {
84 if (pixel_format == FORMAT_R) {
85 internal_format = GL_R16F;
86 } else if (pixel_format == FORMAT_RG) {
87 internal_format = GL_RG16F;
88 } else if (pixel_format == FORMAT_RGB) {
89 internal_format = GL_RGB16F;
91 internal_format = GL_RGBA16F;
93 } else if (type == GL_UNSIGNED_SHORT) {
94 if (pixel_format == FORMAT_R) {
95 internal_format = GL_R16;
96 } else if (pixel_format == FORMAT_RG) {
97 internal_format = GL_RG16;
98 } else if (pixel_format == FORMAT_RGB) {
99 internal_format = GL_RGB16;
101 internal_format = GL_RGBA16;
103 } else if (output_linear_gamma) {
104 assert(type == GL_UNSIGNED_BYTE);
105 if (pixel_format == FORMAT_RGB) {
106 internal_format = GL_SRGB8;
107 } else if (pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
108 internal_format = GL_SRGB8_ALPHA8;
113 assert(type == GL_UNSIGNED_BYTE);
114 if (pixel_format == FORMAT_R) {
115 internal_format = GL_R8;
116 } else if (pixel_format == FORMAT_RG) {
117 internal_format = GL_RG8;
118 } else if (pixel_format == FORMAT_RGB) {
119 internal_format = GL_RGB8;
121 internal_format = GL_RGBA8;
124 if (pixel_format == FORMAT_RGB) {
126 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
127 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
129 } else if (pixel_format == FORMAT_RG) {
131 } else if (pixel_format == FORMAT_R) {
137 // (Re-)upload the texture.
138 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
139 glBindTexture(GL_TEXTURE_2D, texture_num);
141 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
145 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
147 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
149 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
151 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
154 glGenerateMipmap(GL_TEXTURE_2D);
157 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
163 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 possibly_release_texture();
189 void FlatInput::possibly_release_texture()
191 if (texture_num != 0 && owns_texture) {
192 resource_pool->release_2d_texture(texture_num);
194 owns_texture = false;