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