X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=effect_chain.cpp;h=50fe599280f0c95350f67d059b5c845b6042bda6;hb=8f070dcdd579e19e092a6619115f4b084fbb2c33;hp=05adfd835a5b2189e3e5da319c12dd47ac5ff45a;hpb=a88f299483ffe5068cd2828513078b9103325da8;p=movit diff --git a/effect_chain.cpp b/effect_chain.cpp index 05adfd8..50fe599 100644 --- a/effect_chain.cpp +++ b/effect_chain.cpp @@ -1,12 +1,20 @@ +#define GL_GLEXT_PROTOTYPES 1 + +#include +#include #include +#include +#include + +#include "util.h" #include "effect_chain.h" #include "gamma_expansion_effect.h" #include "lift_gamma_gain_effect.h" #include "colorspace_conversion_effect.h" EffectChain::EffectChain(unsigned width, unsigned height) - : width(width), height(height) {} + : width(width), height(height), finalized(false) {} void EffectChain::add_input(const ImageFormat &format) { @@ -53,7 +61,81 @@ Effect *EffectChain::add_effect(EffectId effect_id) current_color_space = COLORSPACE_sRGB; } + // not handled yet + assert(!effect->needs_many_samples()); + assert(!effect->needs_mipmaps()); + effects.push_back(effect); return effect; } +// GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with _x. +std::string replace_prefix(const std::string &text, const std::string &prefix) +{ + std::string output; + size_t start = 0; + + while (start < text.size()) { + size_t pos = text.find("PREFIX(", start); + if (pos == std::string::npos) { + output.append(text.substr(start, std::string::npos)); + break; + } + + output.append(text.substr(start, pos - start)); + output.append(prefix); + output.append("_"); + + pos += strlen("PREFIX("); + + // Output stuff until we find the matching ), which we then eat. + int depth = 1; + size_t end_arg_pos = pos; + while (end_arg_pos < text.size()) { + if (text[end_arg_pos] == '(') { + ++depth; + } else if (text[end_arg_pos] == ')') { + --depth; + if (depth == 0) { + break; + } + } + ++end_arg_pos; + } + output.append(text.substr(pos, end_arg_pos - pos)); + ++end_arg_pos; + assert(depth == 0); + start = end_arg_pos; + } + return output; +} + +void EffectChain::finalize() +{ + std::string frag_shader = read_file("header.glsl"); + + for (unsigned i = 0; i < effects.size(); ++i) { + char effect_id[256]; + sprintf(effect_id, "eff%d", i); + + frag_shader += "\n"; + frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n"; + frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id); + frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id); + frag_shader += "#undef PREFIX\n"; + frag_shader += "#undef FUNCNAME\n"; + frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n"; + frag_shader += "\n"; + } + printf("%s\n", frag_shader.c_str()); + + glsl_program_num = glCreateProgram(); + GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER); + GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER); + glAttachObjectARB(glsl_program_num, vs_obj); + check_error(); + glAttachObjectARB(glsl_program_num, fs_obj); + check_error(); + glLinkProgram(glsl_program_num); + check_error(); +}