Make it possible to instantiate the colorspace conversion effect separately.
[movit] / effect_chain.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #include <GL/gl.h>
8 #include <GL/glext.h>
9
10 #include "util.h"
11 #include "effect_chain.h"
12 #include "gamma_expansion_effect.h"
13 #include "gamma_compression_effect.h"
14 #include "lift_gamma_gain_effect.h"
15 #include "colorspace_conversion_effect.h"
16 #include "saturation_effect.h"
17 #include "texture_enum.h"
18
19 EffectChain::EffectChain(unsigned width, unsigned height)
20         : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
21
22 void EffectChain::add_input(const ImageFormat &format)
23 {
24         input_format = format;
25         current_color_space = format.color_space;
26         current_gamma_curve = format.gamma_curve;
27 }
28
29 void EffectChain::add_output(const ImageFormat &format)
30 {
31         output_format = format;
32 }
33         
34 Effect *instantiate_effect(EffectId effect)
35 {
36         switch (effect) {
37         case EFFECT_GAMMA_EXPANSION:
38                 return new GammaExpansionEffect();
39         case EFFECT_GAMMA_COMPRESSION:
40                 return new GammaCompressionEffect();
41         case EFFECT_COLOR_SPACE_CONVERSION:
42                 return new ColorSpaceConversionEffect();
43         case EFFECT_LIFT_GAMMA_GAIN:
44                 return new LiftGammaGainEffect();
45         case EFFECT_SATURATION:
46                 return new SaturationEffect();
47         }
48         assert(false);
49 }
50
51 void EffectChain::normalize_to_linear_gamma()
52 {
53         if (current_gamma_curve == GAMMA_sRGB) {
54                 // TODO: check if the extension exists
55                 use_srgb_texture_format = true;
56         } else {
57                 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
58                 gamma_conversion->set_int("source_curve", current_gamma_curve);
59                 effects.push_back(gamma_conversion);
60         }
61         current_gamma_curve = GAMMA_LINEAR;
62 }
63
64 void EffectChain::normalize_to_srgb()
65 {
66         assert(current_gamma_curve == GAMMA_LINEAR);
67         ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
68         colorspace_conversion->set_int("source_space", current_color_space);
69         colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
70         effects.push_back(colorspace_conversion);
71         current_color_space = COLORSPACE_sRGB;
72 }
73
74 Effect *EffectChain::add_effect(EffectId effect_id)
75 {
76         Effect *effect = instantiate_effect(effect_id);
77
78         if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
79                 normalize_to_linear_gamma();
80         }
81
82         if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
83                 normalize_to_srgb();
84         }
85
86         // not handled yet
87         assert(!effect->needs_many_samples());
88         assert(!effect->needs_mipmaps());
89
90         effects.push_back(effect);
91         return effect;
92 }
93
94 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
95 std::string replace_prefix(const std::string &text, const std::string &prefix)
96 {
97         std::string output;
98         size_t start = 0;
99
100         while (start < text.size()) {
101                 size_t pos = text.find("PREFIX(", start);
102                 if (pos == std::string::npos) {
103                         output.append(text.substr(start, std::string::npos));
104                         break;
105                 }
106
107                 output.append(text.substr(start, pos - start));
108                 output.append(prefix);
109                 output.append("_");
110
111                 pos += strlen("PREFIX(");
112         
113                 // Output stuff until we find the matching ), which we then eat.
114                 int depth = 1;
115                 size_t end_arg_pos = pos;
116                 while (end_arg_pos < text.size()) {
117                         if (text[end_arg_pos] == '(') {
118                                 ++depth;
119                         } else if (text[end_arg_pos] == ')') {
120                                 --depth;
121                                 if (depth == 0) {
122                                         break;
123                                 }
124                         }
125                         ++end_arg_pos;
126                 }
127                 output.append(text.substr(pos, end_arg_pos - pos));
128                 ++end_arg_pos;
129                 assert(depth == 0);
130                 start = end_arg_pos;
131         }
132         return output;
133 }
134
135 void EffectChain::finalize()
136 {
137         if (current_color_space != output_format.color_space) {
138                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
139                 colorspace_conversion->set_int("source_space", current_color_space);
140                 colorspace_conversion->set_int("destination_space", output_format.color_space);
141                 effects.push_back(colorspace_conversion);
142                 current_color_space = output_format.color_space;
143         }
144
145         if (current_gamma_curve != output_format.gamma_curve) {
146                 if (current_gamma_curve != GAMMA_LINEAR) {
147                         normalize_to_linear_gamma();
148                 }
149                 assert(current_gamma_curve == GAMMA_LINEAR);
150                 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
151                 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
152                 effects.push_back(gamma_conversion);
153                 current_gamma_curve = output_format.gamma_curve;
154         }
155
156         std::string frag_shader = read_file("header.glsl");
157
158         for (unsigned i = 0; i < effects.size(); ++i) {
159                 char effect_id[256];
160                 sprintf(effect_id, "eff%d", i);
161         
162                 frag_shader += "\n";
163                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
164                 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
165                 frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
166                 frag_shader += "#undef PREFIX\n";
167                 frag_shader += "#undef FUNCNAME\n";
168                 frag_shader += "#undef LAST_INPUT\n";
169                 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
170                 frag_shader += "\n";
171         }
172         frag_shader.append(read_file("footer.glsl"));
173         printf("%s\n", frag_shader.c_str());
174         
175         glsl_program_num = glCreateProgram();
176         GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
177         GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
178         glAttachShader(glsl_program_num, vs_obj);
179         check_error();
180         glAttachShader(glsl_program_num, fs_obj);
181         check_error();
182         glLinkProgram(glsl_program_num);
183         check_error();
184
185         finalized = true;
186 }
187
188 void EffectChain::render_to_screen(unsigned char *src)
189 {
190         assert(finalized);
191
192         check_error();
193         glUseProgram(glsl_program_num);
194         check_error();
195
196         glActiveTexture(GL_TEXTURE0);
197         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
198
199         GLenum internal_format = GL_RGBA8;
200         if (use_srgb_texture_format) {
201                 internal_format = GL_SRGB8;
202         }
203
204         if (input_format.pixel_format == FORMAT_RGB) {
205                 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
206         } else if (input_format.pixel_format == FORMAT_RGBA) {
207                 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
208         } else {
209                 assert(false);
210         }
211         check_error();
212         glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
213
214         for (unsigned i = 0; i < effects.size(); ++i) {
215                 char effect_id[256];
216                 sprintf(effect_id, "eff%d", i);
217                 effects[i]->set_uniforms(glsl_program_num, effect_id);
218         }
219
220         glDisable(GL_BLEND);
221         check_error();
222         glDisable(GL_DEPTH_TEST);
223         check_error();
224         glDepthMask(GL_FALSE);
225         check_error();
226
227         glMatrixMode(GL_PROJECTION);
228         glLoadIdentity();
229         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
230
231         glMatrixMode(GL_MODELVIEW);
232         glLoadIdentity();
233
234         glBegin(GL_QUADS);
235
236         glTexCoord2f(0.0f, 1.0f);
237         glVertex2f(0.0f, 0.0f);
238
239         glTexCoord2f(1.0f, 1.0f);
240         glVertex2f(1.0f, 0.0f);
241
242         glTexCoord2f(1.0f, 0.0f);
243         glVertex2f(1.0f, 1.0f);
244
245         glTexCoord2f(0.0f, 0.0f);
246         glVertex2f(0.0f, 1.0f);
247
248         glEnd();
249         check_error();
250 }