]> git.sesse.net Git - movit/blob - effect_chain.cpp
d45c2fc4f86ba5c76bad0aa0da52129f05f25b3d
[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 frag_shader = read_file("header.frag");
163         for (unsigned i = 0; i < effects.size(); ++i) {
164                 char effect_id[256];
165                 sprintf(effect_id, "eff%d", i);
166         
167                 frag_shader += "\n";
168                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
169                 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
170                 frag_shader += replace_prefix(effects[i]->output_fragment_shader(), effect_id);
171                 frag_shader += "#undef PREFIX\n";
172                 frag_shader += "#undef FUNCNAME\n";
173                 frag_shader += "#undef LAST_INPUT\n";
174                 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
175                 frag_shader += "\n";
176         }
177         frag_shader.append(read_file("footer.frag"));
178         printf("%s\n", frag_shader.c_str());
179         
180         glsl_program_num = glCreateProgram();
181         GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
182         GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
183         glAttachShader(glsl_program_num, vs_obj);
184         check_error();
185         glAttachShader(glsl_program_num, fs_obj);
186         check_error();
187         glLinkProgram(glsl_program_num);
188         check_error();
189
190         // Translate the format to OpenGL's enums.
191         GLenum internal_format;
192         if (use_srgb_texture_format) {
193                 internal_format = GL_SRGB8;
194         } else {
195                 internal_format = GL_RGBA8;
196         }
197         if (input_format.pixel_format == FORMAT_RGB) {
198                 format = GL_RGB;
199                 bytes_per_pixel = 3;
200         } else if (input_format.pixel_format == FORMAT_RGBA) {
201                 format = GL_RGBA;
202                 bytes_per_pixel = 4;
203         } else if (input_format.pixel_format == FORMAT_BGR) {
204                 format = GL_BGR;
205                 bytes_per_pixel = 3;
206         } else if (input_format.pixel_format == FORMAT_BGRA) {
207                 format = GL_BGRA;
208                 bytes_per_pixel = 4;
209         } else {
210                 assert(false);
211         }
212
213         // Create PBO to hold the texture, and then the texture itself.
214         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
215         check_error();
216         glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
217         check_error();
218
219         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
220         memset(mapped_pbo, 0, width * height * bytes_per_pixel);
221         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
222         
223         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
224         check_error();
225         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
226         check_error();
227         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
228         check_error();
229
230         finalized = true;
231 }
232
233 void EffectChain::render_to_screen(unsigned char *src)
234 {
235         assert(finalized);
236
237         // Copy the pixel data into the PBO.
238         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
239         check_error();
240         void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
241         memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
242         glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
243         check_error();
244
245         // Re-upload the texture from the PBO.
246         glActiveTexture(GL_TEXTURE0);
247         check_error();
248         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
249         check_error();
250         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
251         check_error();
252         glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
253         check_error();
254
255         glUseProgram(glsl_program_num);
256         check_error();
257
258         check_error();
259         glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
260
261         for (unsigned i = 0; i < effects.size(); ++i) {
262                 char effect_id[256];
263                 sprintf(effect_id, "eff%d", i);
264                 effects[i]->set_uniforms(glsl_program_num, effect_id);
265         }
266
267         glDisable(GL_BLEND);
268         check_error();
269         glDisable(GL_DEPTH_TEST);
270         check_error();
271         glDepthMask(GL_FALSE);
272         check_error();
273
274         glMatrixMode(GL_PROJECTION);
275         glLoadIdentity();
276         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
277
278         glMatrixMode(GL_MODELVIEW);
279         glLoadIdentity();
280
281         glBegin(GL_QUADS);
282
283         glTexCoord2f(0.0f, 1.0f);
284         glVertex2f(0.0f, 0.0f);
285
286         glTexCoord2f(1.0f, 1.0f);
287         glVertex2f(1.0f, 0.0f);
288
289         glTexCoord2f(1.0f, 0.0f);
290         glVertex2f(1.0f, 1.0f);
291
292         glTexCoord2f(0.0f, 0.0f);
293         glVertex2f(0.0f, 1.0f);
294
295         glEnd();
296         check_error();
297 }