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