8 #include "effect_util.h"
9 #include "resource_pool.h"
12 #include "ycbcr_input.h"
14 using namespace Eigen;
19 YCbCrInput::YCbCrInput(const ImageFormat &image_format,
20 const YCbCrFormat &ycbcr_format,
21 unsigned width, unsigned height,
22 YCbCrInputSplitting ycbcr_input_splitting,
24 : image_format(image_format),
25 ycbcr_format(ycbcr_format),
26 ycbcr_input_splitting(ycbcr_input_splitting),
32 pbos[0] = pbos[1] = pbos[2] = 0;
33 texture_num[0] = texture_num[1] = texture_num[2] = 0;
38 pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
39 owns_texture[0] = owns_texture[1] = owns_texture[2] = false;
41 register_uniform_sampler2d("tex_y", &uniform_tex_y);
43 if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
45 assert(ycbcr_format.chroma_subsampling_x == 1);
46 assert(ycbcr_format.chroma_subsampling_y == 1);
47 } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
49 register_uniform_sampler2d("tex_cbcr", &uniform_tex_cb);
51 assert(ycbcr_input_splitting == YCBCR_INPUT_PLANAR);
53 register_uniform_sampler2d("tex_cb", &uniform_tex_cb);
54 register_uniform_sampler2d("tex_cr", &uniform_tex_cr);
58 YCbCrInput::~YCbCrInput()
60 for (unsigned channel = 0; channel < num_channels; ++channel) {
61 possibly_release_texture(channel);
65 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
67 for (unsigned channel = 0; channel < num_channels; ++channel) {
68 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
71 if (texture_num[channel] == 0 && (pbos[channel] != 0 || pixel_data[channel] != NULL)) {
72 GLenum format, internal_format;
73 if (channel == 0 && ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
74 if (type == GL_UNSIGNED_INT_2_10_10_10_REV) {
76 internal_format = GL_RGB10_A2;
78 assert(type == GL_UNSIGNED_BYTE);
80 internal_format = GL_RGB8;
82 } else if (channel == 1 && ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
83 assert(type == GL_UNSIGNED_BYTE);
85 internal_format = GL_RG8;
87 assert(type == GL_UNSIGNED_BYTE);
89 internal_format = GL_R8;
92 // (Re-)upload the texture.
93 texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], heights[channel]);
94 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
98 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
100 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
102 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
104 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], format, type, pixel_data[channel]);
106 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
112 owns_texture[channel] = true;
114 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
119 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
123 uniform_tex_y = *sampler_num + 0;
124 uniform_tex_cb = *sampler_num + 1;
125 if (ycbcr_input_splitting == YCBCR_INPUT_PLANAR) {
126 uniform_tex_cr = *sampler_num + 2;
129 *sampler_num += num_channels;
132 string YCbCrInput::output_fragment_shader()
135 Matrix3d ycbcr_to_rgb;
136 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
140 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
141 frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
143 float cb_offset_x = compute_chroma_offset(
144 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
145 float cb_offset_y = compute_chroma_offset(
146 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
147 frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
149 float cr_offset_x = compute_chroma_offset(
150 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
151 float cr_offset_y = compute_chroma_offset(
152 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
153 frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
155 if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
156 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 1\n";
157 } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
159 snprintf(buf, sizeof(buf), "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 1\n#define CB_CR_OFFSETS_EQUAL %d\n",
160 (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
163 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 0\n";
166 frag_shader += read_file("ycbcr_input.frag");
170 void YCbCrInput::invalidate_pixel_data()
172 for (unsigned channel = 0; channel < 3; ++channel) {
173 possibly_release_texture(channel);
177 bool YCbCrInput::set_int(const std::string& key, int value)
179 if (key == "needs_mipmaps") {
180 // We currently do not support this.
183 return Effect::set_int(key, value);
186 void YCbCrInput::possibly_release_texture(unsigned channel)
188 if (texture_num[channel] != 0 && owns_texture[channel]) {
189 resource_pool->release_2d_texture(texture_num[channel]);
190 texture_num[channel] = 0;
191 owns_texture[channel] = false;