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 : image_format(image_format),
23 ycbcr_format(ycbcr_format),
28 pbos[0] = pbos[1] = pbos[2] = 0;
29 texture_num[0] = texture_num[1] = texture_num[2] = 0;
31 assert(width % ycbcr_format.chroma_subsampling_x == 0);
32 pitch[0] = widths[0] = width;
33 pitch[1] = widths[1] = width / ycbcr_format.chroma_subsampling_x;
34 pitch[2] = widths[2] = width / ycbcr_format.chroma_subsampling_x;
36 assert(height % ycbcr_format.chroma_subsampling_y == 0);
38 heights[1] = height / ycbcr_format.chroma_subsampling_y;
39 heights[2] = height / ycbcr_format.chroma_subsampling_y;
41 pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
43 register_uniform_sampler2d("tex_y", &uniform_tex_y);
44 register_uniform_sampler2d("tex_cb", &uniform_tex_cb);
45 register_uniform_sampler2d("tex_cr", &uniform_tex_cr);
48 YCbCrInput::~YCbCrInput()
50 for (unsigned channel = 0; channel < 3; ++channel) {
51 if (texture_num[channel] != 0) {
52 resource_pool->release_2d_texture(texture_num[channel]);
57 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
59 for (unsigned channel = 0; channel < 3; ++channel) {
60 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
63 if (texture_num[channel] == 0) {
64 // (Re-)upload the texture.
65 texture_num[channel] = resource_pool->create_2d_texture(GL_R8, widths[channel], heights[channel]);
66 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
70 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
72 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
74 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
76 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_RED, GL_UNSIGNED_BYTE, pixel_data[channel]);
78 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
85 glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
90 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
94 uniform_tex_y = *sampler_num + 0;
95 uniform_tex_cb = *sampler_num + 1;
96 uniform_tex_cr = *sampler_num + 2;
101 string YCbCrInput::output_fragment_shader()
104 Matrix3d ycbcr_to_rgb;
105 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
109 frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
110 frag_shader += output_glsl_vec3("PREFIX(offset)", offset[0], offset[1], offset[2]);
112 float cb_offset_x = compute_chroma_offset(
113 ycbcr_format.cb_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
114 float cb_offset_y = compute_chroma_offset(
115 ycbcr_format.cb_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
116 frag_shader += output_glsl_vec2("PREFIX(cb_offset)", cb_offset_x, cb_offset_y);
118 float cr_offset_x = compute_chroma_offset(
119 ycbcr_format.cr_x_position, ycbcr_format.chroma_subsampling_x, widths[2]);
120 float cr_offset_y = compute_chroma_offset(
121 ycbcr_format.cr_y_position, ycbcr_format.chroma_subsampling_y, heights[2]);
122 frag_shader += output_glsl_vec2("PREFIX(cr_offset)", cr_offset_x, cr_offset_y);
124 frag_shader += read_file("ycbcr_input.frag");
128 void YCbCrInput::invalidate_pixel_data()
130 for (unsigned channel = 0; channel < 3; ++channel) {
131 if (texture_num[channel] != 0) {
132 resource_pool->release_2d_texture(texture_num[channel]);
133 texture_num[channel] = 0;
138 bool YCbCrInput::set_int(const std::string& key, int value)
140 if (key == "needs_mipmaps") {
141 // We currently do not support this.
144 return Effect::set_int(key, value);