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