]> git.sesse.net Git - movit/blob - effect_chain.cpp
Revert "Move calculation of normalized position for the vignette into the vertex...
[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 "mirror_effect.h"
18 #include "vignette_effect.h"
19 #include "texture_enum.h"
20
21 EffectChain::EffectChain(unsigned width, unsigned height)
22         : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
23
24 void EffectChain::add_input(const ImageFormat &format)
25 {
26         input_format = format;
27         current_color_space = format.color_space;
28         current_gamma_curve = format.gamma_curve;
29 }
30
31 void EffectChain::add_output(const ImageFormat &format)
32 {
33         output_format = format;
34 }
35         
36 Effect *instantiate_effect(EffectId effect)
37 {
38         switch (effect) {
39         case EFFECT_GAMMA_EXPANSION:
40                 return new GammaExpansionEffect();
41         case EFFECT_GAMMA_COMPRESSION:
42                 return new GammaCompressionEffect();
43         case EFFECT_COLOR_SPACE_CONVERSION:
44                 return new ColorSpaceConversionEffect();
45         case EFFECT_LIFT_GAMMA_GAIN:
46                 return new LiftGammaGainEffect();
47         case EFFECT_SATURATION:
48                 return new SaturationEffect();
49         case EFFECT_MIRROR:
50                 return new MirrorEffect();
51         case EFFECT_VIGNETTE:
52                 return new VignetteEffect();
53         }
54         assert(false);
55 }
56
57 void EffectChain::normalize_to_linear_gamma()
58 {
59         if (current_gamma_curve == GAMMA_sRGB) {
60                 // TODO: check if the extension exists
61                 use_srgb_texture_format = true;
62         } else {
63                 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
64                 gamma_conversion->set_int("source_curve", current_gamma_curve);
65                 effects.push_back(gamma_conversion);
66         }
67         current_gamma_curve = GAMMA_LINEAR;
68 }
69
70 void EffectChain::normalize_to_srgb()
71 {
72         assert(current_gamma_curve == GAMMA_LINEAR);
73         ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
74         colorspace_conversion->set_int("source_space", current_color_space);
75         colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
76         effects.push_back(colorspace_conversion);
77         current_color_space = COLORSPACE_sRGB;
78 }
79
80 Effect *EffectChain::add_effect(EffectId effect_id)
81 {
82         Effect *effect = instantiate_effect(effect_id);
83
84         if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
85                 normalize_to_linear_gamma();
86         }
87
88         if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
89                 normalize_to_srgb();
90         }
91
92         // not handled yet
93         assert(!effect->needs_many_samples());
94         assert(!effect->needs_mipmaps());
95
96         effects.push_back(effect);
97         return effect;
98 }
99
100 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
101 std::string replace_prefix(const std::string &text, const std::string &prefix)
102 {
103         std::string output;
104         size_t start = 0;
105
106         while (start < text.size()) {
107                 size_t pos = text.find("PREFIX(", start);
108                 if (pos == std::string::npos) {
109                         output.append(text.substr(start, std::string::npos));
110                         break;
111                 }
112
113                 output.append(text.substr(start, pos - start));
114                 output.append(prefix);
115                 output.append("_");
116
117                 pos += strlen("PREFIX(");
118         
119                 // Output stuff until we find the matching ), which we then eat.
120                 int depth = 1;
121                 size_t end_arg_pos = pos;
122                 while (end_arg_pos < text.size()) {
123                         if (text[end_arg_pos] == '(') {
124                                 ++depth;
125                         } else if (text[end_arg_pos] == ')') {
126                                 --depth;
127                                 if (depth == 0) {
128                                         break;
129                                 }
130                         }
131                         ++end_arg_pos;
132                 }
133                 output.append(text.substr(pos, end_arg_pos - pos));
134                 ++end_arg_pos;
135                 assert(depth == 0);
136                 start = end_arg_pos;
137         }
138         return output;
139 }
140
141 void EffectChain::finalize()
142 {
143         if (current_color_space != output_format.color_space) {
144                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
145                 colorspace_conversion->set_int("source_space", current_color_space);
146                 colorspace_conversion->set_int("destination_space", output_format.color_space);
147                 effects.push_back(colorspace_conversion);
148                 current_color_space = output_format.color_space;
149         }
150
151         if (current_gamma_curve != output_format.gamma_curve) {
152                 if (current_gamma_curve != GAMMA_LINEAR) {
153                         normalize_to_linear_gamma();
154                 }
155                 assert(current_gamma_curve == GAMMA_LINEAR);
156                 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
157                 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
158                 effects.push_back(gamma_conversion);
159                 current_gamma_curve = output_format.gamma_curve;
160         }
161         
162         std::string vert_shader = read_file("header.vert");
163         for (unsigned i = 0; i < effects.size(); ++i) {
164                 char effect_id[256];
165                 sprintf(effect_id, "eff%d", i);
166         
167                 vert_shader += "\n";
168                 vert_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
169                 vert_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
170                 vert_shader += replace_prefix(effects[i]->output_vertex_shader(), effect_id);
171                 vert_shader += "#undef PREFIX\n";
172                 vert_shader += "#undef FUNCNAME\n";
173                 vert_shader += "#undef LAST_INPUT\n";
174                 vert_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
175                 vert_shader += "\n";
176         }
177         vert_shader.append(read_file("footer.vert"));
178         printf("%s\n", vert_shader.c_str());
179
180         std::string frag_shader = read_file("header.frag");
181         for (unsigned i = 0; i < effects.size(); ++i) {
182                 char effect_id[256];
183                 sprintf(effect_id, "eff%d", i);
184         
185                 frag_shader += "\n";
186                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
187                 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
188                 frag_shader += replace_prefix(effects[i]->output_fragment_shader(), effect_id);
189                 frag_shader += "#undef PREFIX\n";
190                 frag_shader += "#undef FUNCNAME\n";
191                 frag_shader += "#undef LAST_INPUT\n";
192                 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
193                 frag_shader += "\n";
194         }
195         frag_shader.append(read_file("footer.frag"));
196         printf("%s\n", frag_shader.c_str());
197         
198         glsl_program_num = glCreateProgram();
199         GLuint vs_obj = compile_shader(vert_shader, GL_VERTEX_SHADER);
200         GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
201         glAttachShader(glsl_program_num, vs_obj);
202         check_error();
203         glAttachShader(glsl_program_num, fs_obj);
204         check_error();
205         glLinkProgram(glsl_program_num);
206         check_error();
207
208         // Translate the format to OpenGL's enums.
209         GLenum internal_format;
210         if (use_srgb_texture_format) {
211                 internal_format = GL_SRGB8;
212         } else {
213                 internal_format = GL_RGBA8;
214         }
215         if (input_format.pixel_format == FORMAT_RGB) {
216                 format = GL_RGB;
217                 bytes_per_pixel = 3;
218         } else if (input_format.pixel_format == FORMAT_RGBA) {
219                 format = GL_RGBA;
220                 bytes_per_pixel = 4;
221         } else if (input_format.pixel_format == FORMAT_BGR) {
222                 format = GL_BGR;
223                 bytes_per_pixel = 3;
224         } else if (input_format.pixel_format == FORMAT_BGRA) {
225                 format = GL_BGRA;
226                 bytes_per_pixel = 4;
227         } else {
228                 assert(false);
229         }
230
231         // Create PBO to hold the texture, and then the texture itself.
232         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
233         check_error();
234         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
235         check_error();
236
237         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
238         memset(mapped_pbo, 0, width * height * bytes_per_pixel);
239         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
240         
241         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
242         check_error();
243         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
244         check_error();
245         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
246         check_error();
247
248         finalized = true;
249 }
250
251 void EffectChain::render_to_screen(unsigned char *src)
252 {
253         assert(finalized);
254
255         // Copy the pixel data into the PBO.
256         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
257         check_error();
258         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
259         memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
260         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
261         check_error();
262
263         // Re-upload the texture from the PBO.
264         glActiveTexture(GL_TEXTURE0);
265         check_error();
266         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
267         check_error();
268         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
269         check_error();
270         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
271         check_error();
272
273         glUseProgram(glsl_program_num);
274         check_error();
275
276         check_error();
277         glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
278
279         for (unsigned i = 0; i < effects.size(); ++i) {
280                 char effect_id[256];
281                 sprintf(effect_id, "eff%d", i);
282                 effects[i]->set_uniforms(glsl_program_num, effect_id);
283         }
284
285         glDisable(GL_BLEND);
286         check_error();
287         glDisable(GL_DEPTH_TEST);
288         check_error();
289         glDepthMask(GL_FALSE);
290         check_error();
291
292         glMatrixMode(GL_PROJECTION);
293         glLoadIdentity();
294         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
295
296         glMatrixMode(GL_MODELVIEW);
297         glLoadIdentity();
298
299         glBegin(GL_QUADS);
300
301         glTexCoord2f(0.0f, 1.0f);
302         glVertex2f(0.0f, 0.0f);
303
304         glTexCoord2f(1.0f, 1.0f);
305         glVertex2f(1.0f, 0.0f);
306
307         glTexCoord2f(1.0f, 0.0f);
308         glVertex2f(1.0f, 1.0f);
309
310         glTexCoord2f(0.0f, 0.0f);
311         glVertex2f(0.0f, 1.0f);
312
313         glEnd();
314         check_error();
315 }