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