1 #define GL_GLEXT_PROTOTYPES 1
11 #include "effect_chain.h"
12 #include "gamma_expansion_effect.h"
13 #include "lift_gamma_gain_effect.h"
14 #include "colorspace_conversion_effect.h"
16 EffectChain::EffectChain(unsigned width, unsigned height)
17 : width(width), height(height), finalized(false) {}
19 void EffectChain::add_input(const ImageFormat &format)
21 input_format = format;
22 current_color_space = format.color_space;
23 current_gamma_curve = format.gamma_curve;
26 void EffectChain::add_output(const ImageFormat &format)
28 output_format = format;
31 Effect *instantiate_effect(EffectId effect)
34 case GAMMA_CONVERSION:
35 return new GammaExpansionEffect();
36 case RGB_PRIMARIES_CONVERSION:
37 return new GammaExpansionEffect();
39 return new LiftGammaGainEffect();
44 Effect *EffectChain::add_effect(EffectId effect_id)
46 Effect *effect = instantiate_effect(effect_id);
48 if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
49 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
50 gamma_conversion->set_int("source_curve", current_gamma_curve);
51 effects.push_back(gamma_conversion);
52 current_gamma_curve = GAMMA_LINEAR;
55 if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
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;
65 assert(!effect->needs_many_samples());
66 assert(!effect->needs_mipmaps());
68 effects.push_back(effect);
72 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
73 std::string replace_prefix(const std::string &text, const std::string &prefix)
78 while (start < text.size()) {
79 size_t pos = text.find("PREFIX(", start);
80 if (pos == std::string::npos) {
81 output.append(text.substr(start, std::string::npos));
85 output.append(text.substr(start, pos - start));
86 output.append(prefix);
89 pos += strlen("PREFIX(");
91 // Output stuff until we find the matching ), which we then eat.
93 size_t end_arg_pos = pos;
94 while (end_arg_pos < text.size()) {
95 if (text[end_arg_pos] == '(') {
97 } else if (text[end_arg_pos] == ')') {
105 output.append(text.substr(pos, end_arg_pos - pos));
113 void EffectChain::finalize()
115 std::string frag_shader = read_file("header.glsl");
117 for (unsigned i = 0; i < effects.size(); ++i) {
119 sprintf(effect_id, "eff%d", i);
122 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
123 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
124 frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
125 frag_shader += "#undef PREFIX\n";
126 frag_shader += "#undef FUNCNAME\n";
127 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
130 printf("%s\n", frag_shader.c_str());
132 glsl_program_num = glCreateProgram();
133 GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
134 GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
135 glAttachObjectARB(glsl_program_num, vs_obj);
137 glAttachObjectARB(glsl_program_num, fs_obj);
139 glLinkProgram(glsl_program_num);