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);
32 // Some types are not supported in all GL versions (e.g. GLES),
33 // and will corrected into the right format in the shader.
34 switch (pixel_format_in) {
35 case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
36 pixel_format = FORMAT_RGBA_PREMULTIPLIED_ALPHA;
39 case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
40 pixel_format = FORMAT_RGBA_POSTMULTIPLIED_ALPHA;
44 pixel_format = FORMAT_RGB;
47 case FORMAT_GRAYSCALE:
48 pixel_format = FORMAT_R;
49 fixup_red_to_grayscale = true;
52 pixel_format = pixel_format_in;
57 FlatInput::~FlatInput()
59 if (texture_num != 0) {
60 resource_pool->release_2d_texture(texture_num);
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) {
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 internal_format = GL_SRGB8_ALPHA8;
107 assert(type == GL_UNSIGNED_BYTE);
108 if (pixel_format == FORMAT_R) {
109 internal_format = GL_R8;
110 } else if (pixel_format == FORMAT_RG) {
111 internal_format = GL_RG8;
112 } else if (pixel_format == FORMAT_RGB) {
113 internal_format = GL_RGB8;
115 internal_format = GL_RGBA8;
118 if (pixel_format == FORMAT_RGB) {
120 } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
121 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
123 } else if (pixel_format == FORMAT_RG) {
125 } else if (pixel_format == FORMAT_R) {
131 // (Re-)upload the texture.
132 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
133 glBindTexture(GL_TEXTURE_2D, texture_num);
135 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
139 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
141 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
143 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
145 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
148 glGenerateMipmap(GL_TEXTURE_2D);
151 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
155 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
157 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
160 glBindTexture(GL_TEXTURE_2D, texture_num);
164 // Bind it to a sampler.
165 set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
169 string FlatInput::output_fragment_shader()
172 sprintf(buf, "#define FIXUP_SWAP_RB %d\n#define FIXUP_RED_TO_GRAYSCALE %d\n",
173 fixup_swap_rb, fixup_red_to_grayscale);
174 return buf + read_file("flat_input.frag");
177 void FlatInput::invalidate_pixel_data()
179 if (texture_num != 0) {
180 resource_pool->release_2d_texture(texture_num);