]> git.sesse.net Git - movit/blob - ycbcr_input.cpp
Cleanup: Make uniforms for RTT samplers like all other uniforms.
[movit] / ycbcr_input.cpp
1 #include <Eigen/Core>
2 #include <Eigen/LU>
3 #include <epoxy/gl.h>
4 #include <assert.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "effect_util.h"
9 #include "resource_pool.h"
10 #include "util.h"
11 #include "ycbcr.h"
12 #include "ycbcr_input.h"
13
14 using namespace Eigen;
15 using namespace std;
16
17 namespace movit {
18
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),
24           width(width),
25           height(height),
26           resource_pool(NULL)
27 {
28         pbos[0] = pbos[1] = pbos[2] = 0;
29         texture_num[0] = texture_num[1] = texture_num[2] = 0;
30
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;
35
36         assert(height % ycbcr_format.chroma_subsampling_y == 0);
37         heights[0] = height;
38         heights[1] = height / ycbcr_format.chroma_subsampling_y;
39         heights[2] = height / ycbcr_format.chroma_subsampling_y;
40
41         pixel_data[0] = pixel_data[1] = pixel_data[2] = NULL;
42
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);
46 }
47
48 YCbCrInput::~YCbCrInput()
49 {
50         for (unsigned channel = 0; channel < 3; ++channel) {
51                 if (texture_num[channel] != 0) {
52                         resource_pool->release_2d_texture(texture_num[channel]);
53                 }
54         }
55 }
56
57 void YCbCrInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
58 {
59         for (unsigned channel = 0; channel < 3; ++channel) {
60                 glActiveTexture(GL_TEXTURE0 + *sampler_num + channel);
61                 check_error();
62
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]);
67                         check_error();
68                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
69                         check_error();
70                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbos[channel]);
71                         check_error();
72                         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
73                         check_error();
74                         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch[channel]);
75                         check_error();
76                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, widths[channel], heights[channel], GL_RED, GL_UNSIGNED_BYTE, pixel_data[channel]);
77                         check_error();
78                         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
79                         check_error();
80                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
81                         check_error();
82                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
83                         check_error();
84                 } else {
85                         glBindTexture(GL_TEXTURE_2D, texture_num[channel]);
86                         check_error();
87                 }
88         }
89
90         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
91         check_error();
92
93         // Bind samplers.
94         uniform_tex_y = *sampler_num + 0;
95         uniform_tex_cb = *sampler_num + 1;
96         uniform_tex_cr = *sampler_num + 2;
97
98         *sampler_num += 3;
99 }
100
101 string YCbCrInput::output_fragment_shader()
102 {
103         float offset[3];
104         Matrix3d ycbcr_to_rgb;
105         compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
106
107         string frag_shader;
108
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]);
111
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);
117
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);
123
124         frag_shader += read_file("ycbcr_input.frag");
125         return frag_shader;
126 }
127
128 void YCbCrInput::invalidate_pixel_data()
129 {
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;
134                 }
135         }
136 }
137
138 bool YCbCrInput::set_int(const std::string& key, int value)
139 {
140         if (key == "needs_mipmaps") {
141                 // We currently do not support this.
142                 return (value == 0);
143         }
144         return Effect::set_int(key, value);
145 }
146
147 }  // namespace movit