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),
33 pbos[0] = pbos[1] = pbos[2] = 0;
34 texture_num[0] = texture_num[1] = texture_num[2] = 0;
39 pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
40 owns_texture[0] = owns_texture[1] = owns_texture[2] = false;
42 register_uniform_sampler2d("tex_y", &uniform_tex_y);
44 if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
46 assert(ycbcr_format.chroma_subsampling_x == 1);
47 assert(ycbcr_format.chroma_subsampling_y == 1);
48 } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
50 register_uniform_sampler2d("tex_cbcr", &uniform_tex_cb);
52 assert(ycbcr_input_splitting == YCBCR_INPUT_PLANAR);
54 register_uniform_sampler2d("tex_cb", &uniform_tex_cb);
55 register_uniform_sampler2d("tex_cr", &uniform_tex_cr);
58 register_int("needs_mipmaps", &needs_mipmaps);
61 YCbCrInput::~YCbCrInput()
63 for (unsigned channel = 0; channel < num_channels; ++channel) {
64 possibly_release_texture(channel);
68 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
70 for (unsigned channel = 0; channel < num_channels; ++channel) {
71 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
74 if (texture_num[channel] == 0 && (pbos[channel] != 0 || pixel_data[channel] != NULL)) {
75 GLenum format, internal_format;
76 if (channel == 0 && ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
77 if (type == GL_UNSIGNED_INT_2_10_10_10_REV) {
79 internal_format = GL_RGB10_A2;
80 } else if (type == GL_UNSIGNED_SHORT) {
82 internal_format = GL_RGB16;
84 assert(type == GL_UNSIGNED_BYTE);
86 internal_format = GL_RGB8;
88 } else if (channel == 1 && ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
90 if (type == GL_UNSIGNED_SHORT) {
91 internal_format = GL_RG16;
93 assert(type == GL_UNSIGNED_BYTE);
94 internal_format = GL_RG8;
98 if (type == GL_UNSIGNED_SHORT) {
99 internal_format = GL_R16;
101 assert(type == GL_UNSIGNED_BYTE);
102 internal_format = GL_R8;
106 // (Re-)upload the texture.
107 texture_num[channel] = resource_pool->create_2d_texture(internal_format, widths[channel], heights[channel]);
108 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
112 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
114 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
116 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
118 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], format, type, pixel_data[channel]);
120 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
123 glGenerateMipmap(GL_TEXTURE_2D);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
130 owns_texture[channel] = true;
132 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
137 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
141 uniform_tex_y = *sampler_num + 0;
142 uniform_tex_cb = *sampler_num + 1;
143 if (ycbcr_input_splitting == YCBCR_INPUT_PLANAR) {
144 uniform_tex_cr = *sampler_num + 2;
147 *sampler_num += num_channels;
150 string YCbCrInput::output_fragment_shader()
153 Matrix3d ycbcr_to_rgb;
154 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
156 if (type == GL_UNSIGNED_SHORT) {
157 // For 10-bit or 12-bit packed into 16-bit, we need to scale the values
158 // so that the max value goes from 1023 (or 4095) to 65535. We do this
159 // by folding the scaling into the conversion matrix, so it comes essentially
160 // for free. However, the offset is before the scaling (and thus assumes
161 // correctly scaled values), so we need to adjust that the other way.
162 double scale = 65535.0 / (ycbcr_format.num_levels - 1);
166 ycbcr_to_rgb *= scale;
171 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
172 frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
174 float cb_offset_x = compute_chroma_offset(
175 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
176 float cb_offset_y = compute_chroma_offset(
177 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
178 frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
180 float cr_offset_x = compute_chroma_offset(
181 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
182 float cr_offset_y = compute_chroma_offset(
183 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
184 frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
186 if (ycbcr_input_splitting == YCBCR_INPUT_INTERLEAVED) {
187 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 1\n";
188 } else if (ycbcr_input_splitting == YCBCR_INPUT_SPLIT_Y_AND_CBCR) {
190 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",
191 (fabs(ycbcr_format.cb_x_position - ycbcr_format.cr_x_position) < 1e-6));
194 frag_shader += "#define Y_CB_CR_SAME_TEXTURE 0\n#define CB_CR_SAME_TEXTURE 0\n";
197 frag_shader += read_file("ycbcr_input.frag");
201 void YCbCrInput::invalidate_pixel_data()
203 for (unsigned channel = 0; channel < 3; ++channel) {
204 possibly_release_texture(channel);
208 bool YCbCrInput::set_int(const std::string& key, int value)
210 if (key == "needs_mipmaps") {
211 if (ycbcr_input_splitting != YCBCR_INPUT_INTERLEAVED && value != 0) {
212 // We do not currently support this.
216 return Effect::set_int(key, value);
219 void YCbCrInput::possibly_release_texture(unsigned channel)
221 if (texture_num[channel] != 0 && owns_texture[channel]) {
222 resource_pool->release_2d_texture(texture_num[channel]);
223 texture_num[channel] = 0;
224 owns_texture[channel] = false;