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