1 #define GL_GLEXT_PROTOTYPES 1
13 #include "effect_chain.h"
14 #include "gamma_expansion_effect.h"
15 #include "gamma_compression_effect.h"
16 #include "colorspace_conversion_effect.h"
20 EffectChain::EffectChain(unsigned width, unsigned height)
25 Input *EffectChain::add_input(Input *input)
28 sprintf(eff_id, "src_image%u", (unsigned)inputs.size());
30 effects.push_back(input);
31 inputs.push_back(input);
32 output_color_space.insert(std::make_pair(input, input->get_color_space()));
33 output_gamma_curve.insert(std::make_pair(input, input->get_gamma_curve()));
34 effect_ids.insert(std::make_pair(input, eff_id));
35 incoming_links.insert(std::make_pair(input, std::vector<Effect *>()));
39 void EffectChain::add_output(const ImageFormat &format)
41 output_format = format;
44 void EffectChain::add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs)
47 sprintf(effect_id, "eff%u", (unsigned)effects.size());
49 effects.push_back(effect);
50 effect_ids.insert(std::make_pair(effect, effect_id));
51 assert(inputs.size() == effect->num_inputs());
52 for (unsigned i = 0; i < inputs.size(); ++i) {
53 assert(std::find(effects.begin(), effects.end(), inputs[i]) != effects.end());
54 outgoing_links[inputs[i]].push_back(effect);
56 incoming_links.insert(std::make_pair(effect, inputs));
57 output_gamma_curve[effect] = output_gamma_curve[last_added_effect()];
58 output_color_space[effect] = output_color_space[last_added_effect()];
61 void EffectChain::find_all_nonlinear_inputs(Effect *effect,
62 std::vector<Input *> *nonlinear_inputs,
63 std::vector<Effect *> *intermediates)
65 assert(output_gamma_curve.count(effect) != 0);
66 if (output_gamma_curve[effect] == GAMMA_LINEAR) {
69 if (effect->num_inputs() == 0) {
70 nonlinear_inputs->push_back(static_cast<Input *>(effect));
72 intermediates->push_back(effect);
74 assert(incoming_links.count(effect) == 1);
75 std::vector<Effect *> deps = incoming_links[effect];
76 assert(effect->num_inputs() == deps.size());
77 for (unsigned i = 0; i < deps.size(); ++i) {
78 find_all_nonlinear_inputs(deps[i], nonlinear_inputs, intermediates);
83 Effect *EffectChain::normalize_to_linear_gamma(Effect *input)
85 // Find out if all the inputs can be set to deliver sRGB inputs.
86 // If so, we can just ask them to do that instead of inserting a
87 // (possibly expensive) conversion operation.
89 // NOTE: We assume that effects generally don't mess with the gamma
90 // curve (except GammaCompressionEffect, which should never be
91 // inserted into a chain when this is called), so that we can just
92 // update the output gamma as we go.
94 // TODO: Setting this flag for one source might confuse a different
95 // part of the pipeline using the same source.
96 std::vector<Input *> nonlinear_inputs;
97 std::vector<Effect *> intermediates;
98 find_all_nonlinear_inputs(input, &nonlinear_inputs, &intermediates);
101 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
102 all_ok &= nonlinear_inputs[i]->can_output_linear_gamma();
106 for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
107 bool ok = nonlinear_inputs[i]->set_int("output_linear_gamma", 1);
109 output_gamma_curve[nonlinear_inputs[i]] = GAMMA_LINEAR;
111 for (unsigned i = 0; i < intermediates.size(); ++i) {
112 output_gamma_curve[intermediates[i]] = GAMMA_LINEAR;
117 // OK, that didn't work. Insert a conversion effect.
118 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
119 gamma_conversion->set_int("source_curve", output_gamma_curve[input]);
120 std::vector<Effect *> inputs;
121 inputs.push_back(input);
122 gamma_conversion->add_self_to_effect_chain(this, inputs);
123 output_gamma_curve[gamma_conversion] = GAMMA_LINEAR;
124 return gamma_conversion;
127 Effect *EffectChain::normalize_to_srgb(Effect *input)
129 assert(output_gamma_curve.count(input) != 0);
130 assert(output_color_space.count(input) != 0);
131 assert(output_gamma_curve[input] == GAMMA_LINEAR);
132 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
133 colorspace_conversion->set_int("source_space", output_color_space[input]);
134 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
135 std::vector<Effect *> inputs;
136 inputs.push_back(input);
137 colorspace_conversion->add_self_to_effect_chain(this, inputs);
138 output_color_space[colorspace_conversion] = COLORSPACE_sRGB;
139 return colorspace_conversion;
142 Effect *EffectChain::add_effect(Effect *effect, const std::vector<Effect *> &inputs)
144 assert(inputs.size() == effect->num_inputs());
146 std::vector<Effect *> normalized_inputs = inputs;
147 for (unsigned i = 0; i < normalized_inputs.size(); ++i) {
148 assert(output_gamma_curve.count(normalized_inputs[i]) != 0);
149 if (effect->needs_linear_light() && output_gamma_curve[normalized_inputs[i]] != GAMMA_LINEAR) {
150 normalized_inputs[i] = normalize_to_linear_gamma(normalized_inputs[i]);
152 assert(output_color_space.count(normalized_inputs[i]) != 0);
153 if (effect->needs_srgb_primaries() && output_color_space[normalized_inputs[i]] != COLORSPACE_sRGB) {
154 normalized_inputs[i] = normalize_to_srgb(normalized_inputs[i]);
158 effect->add_self_to_effect_chain(this, normalized_inputs);
162 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
163 std::string replace_prefix(const std::string &text, const std::string &prefix)
168 while (start < text.size()) {
169 size_t pos = text.find("PREFIX(", start);
170 if (pos == std::string::npos) {
171 output.append(text.substr(start, std::string::npos));
175 output.append(text.substr(start, pos - start));
176 output.append(prefix);
179 pos += strlen("PREFIX(");
181 // Output stuff until we find the matching ), which we then eat.
183 size_t end_arg_pos = pos;
184 while (end_arg_pos < text.size()) {
185 if (text[end_arg_pos] == '(') {
187 } else if (text[end_arg_pos] == ')') {
195 output.append(text.substr(pos, end_arg_pos - pos));
203 EffectChain::Phase EffectChain::compile_glsl_program(const std::vector<Effect *> &inputs, const std::vector<Effect *> &effects)
205 assert(!effects.empty());
207 // Deduplicate the inputs.
208 std::vector<Effect *> true_inputs = inputs;
209 std::sort(true_inputs.begin(), true_inputs.end());
210 true_inputs.erase(std::unique(true_inputs.begin(), true_inputs.end()), true_inputs.end());
212 bool input_needs_mipmaps = false;
213 std::string frag_shader = read_file("header.frag");
215 // Create functions for all the texture inputs that we need.
216 for (unsigned i = 0; i < true_inputs.size(); ++i) {
217 Effect *effect = true_inputs[i];
218 assert(effect_ids.count(effect) != 0);
219 std::string effect_id = effect_ids[effect];
221 frag_shader += std::string("uniform sampler2D tex_") + effect_id + ";\n";
222 frag_shader += std::string("vec4 ") + effect_id + "(vec2 tc) {\n";
223 if (effect->num_inputs() == 0) {
224 // OpenGL's origin is bottom-left, but most graphics software assumes
225 // a top-left origin. Thus, for inputs that come from the user,
226 // we flip the y coordinate. However, for FBOs, the origin
227 // is all correct, so don't do anything.
228 frag_shader += "\ttc.y = 1.0f - tc.y;\n";
230 frag_shader += "\treturn texture2D(tex_" + effect_id + ", tc);\n";
231 frag_shader += "}\n";
235 std::string last_effect_id;
236 for (unsigned i = 0; i < effects.size(); ++i) {
237 Effect *effect = effects[i];
238 assert(effect != NULL);
239 assert(effect_ids.count(effect) != 0);
240 std::string effect_id = effect_ids[effect];
241 last_effect_id = effect_id;
243 if (incoming_links[effect].size() == 1) {
244 frag_shader += std::string("#define INPUT ") + effect_ids[incoming_links[effect][0]] + "\n";
246 for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
248 sprintf(buf, "#define INPUT%d %s\n", j + 1, effect_ids[incoming_links[effect][j]].c_str());
254 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
255 frag_shader += replace_prefix(effect->output_convenience_uniforms(), effect_id);
256 frag_shader += replace_prefix(effect->output_fragment_shader(), effect_id);
257 frag_shader += "#undef PREFIX\n";
258 frag_shader += "#undef FUNCNAME\n";
259 if (incoming_links[effect].size() == 1) {
260 frag_shader += "#undef INPUT\n";
262 for (unsigned j = 0; j < incoming_links[effect].size(); ++j) {
264 sprintf(buf, "#undef INPUT%d\n", j + 1);
270 input_needs_mipmaps |= effect->needs_mipmaps();
272 for (unsigned i = 0; i < effects.size(); ++i) {
273 Effect *effect = effects[i];
274 if (effect->num_inputs() == 0) {
275 effect->set_int("needs_mipmaps", input_needs_mipmaps);
278 assert(!last_effect_id.empty());
279 frag_shader += std::string("#define INPUT ") + last_effect_id + "\n";
280 frag_shader.append(read_file("footer.frag"));
281 printf("%s\n", frag_shader.c_str());
283 GLuint glsl_program_num = glCreateProgram();
284 GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
285 GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
286 glAttachShader(glsl_program_num, vs_obj);
288 glAttachShader(glsl_program_num, fs_obj);
290 glLinkProgram(glsl_program_num);
294 phase.glsl_program_num = glsl_program_num;
295 phase.input_needs_mipmaps = input_needs_mipmaps;
296 phase.inputs = true_inputs;
297 phase.effects = effects;
302 // Construct GLSL programs, starting at the given effect and following
303 // the chain from there. We end a program every time we come to an effect
304 // marked as "needs texture bounce", one that is used by multiple other
305 // effects, and of course at the end.
307 // We follow a quite simple depth-first search from the output, although
308 // without any explicit recursion.
309 void EffectChain::construct_glsl_programs(Effect *output)
311 // Which effects have already been completed in this phase?
312 // We need to keep track of it, as an effect with multiple outputs
313 // could otherwise be calculate multiple times.
314 std::set<Effect *> completed_effects;
316 // Effects in the current phase, as well as inputs (outputs from other phases
317 // that we depend on). Note that since we start iterating from the end,
318 // the effect list will be in the reverse order.
319 std::vector<Effect *> this_phase_inputs;
320 std::vector<Effect *> this_phase_effects;
322 // Effects that we have yet to calculate, but that we know should
323 // be in the current phase.
324 std::stack<Effect *> effects_todo_this_phase;
326 // Effects that we have yet to calculate, but that come from other phases.
327 // We delay these until we have this phase done in its entirety,
328 // at which point we pick any of them and start a new phase from that.
329 std::stack<Effect *> effects_todo_other_phases;
331 effects_todo_this_phase.push(output);
333 for ( ;; ) { // Termination condition within loop.
334 if (!effects_todo_this_phase.empty()) {
335 // OK, we have more to do this phase.
336 Effect *effect = effects_todo_this_phase.top();
337 effects_todo_this_phase.pop();
339 // This should currently only happen for effects that are phase outputs,
340 // and we throw those out separately below.
341 assert(completed_effects.count(effect) == 0);
343 this_phase_effects.push_back(effect);
344 completed_effects.insert(effect);
346 // Find all the dependencies of this effect, and add them to the stack.
347 assert(incoming_links.count(effect) == 1);
348 std::vector<Effect *> deps = incoming_links[effect];
349 assert(effect->num_inputs() == deps.size());
350 for (unsigned i = 0; i < deps.size(); ++i) {
351 bool start_new_phase = false;
353 if (effect->needs_texture_bounce()) {
354 start_new_phase = true;
357 assert(outgoing_links.count(deps[i]) == 1);
358 if (outgoing_links[deps[i]].size() > 1 && deps[i]->num_inputs() > 0) {
359 // More than one effect uses this as the input,
360 // and it is not a texture itself.
361 // The easiest thing to do (and probably also the safest
362 // performance-wise in most cases) is to bounce it to a texture
363 // and then let the next passes read from that.
364 start_new_phase = true;
367 if (start_new_phase) {
368 effects_todo_other_phases.push(deps[i]);
369 this_phase_inputs.push_back(deps[i]);
371 effects_todo_this_phase.push(deps[i]);
377 // No more effects to do this phase. Take all the ones we have,
378 // and create a GLSL program for it.
379 if (!this_phase_effects.empty()) {
380 reverse(this_phase_effects.begin(), this_phase_effects.end());
381 phases.push_back(compile_glsl_program(this_phase_inputs, this_phase_effects));
382 this_phase_inputs.clear();
383 this_phase_effects.clear();
385 assert(this_phase_inputs.empty());
386 assert(this_phase_effects.empty());
388 // If we have no effects left, exit.
389 if (effects_todo_other_phases.empty()) {
393 Effect *effect = effects_todo_other_phases.top();
394 effects_todo_other_phases.pop();
396 if (completed_effects.count(effect) == 0) {
397 // Start a new phase, calculating from this effect.
398 effects_todo_this_phase.push(effect);
402 // Finally, since the phases are found from the output but must be executed
403 // from the input(s), reverse them, too.
404 std::reverse(phases.begin(), phases.end());
407 void EffectChain::finalize()
409 // Find the output effect. This is, simply, one that has no outgoing links.
410 // If there are multiple ones, the graph is malformed (we do not support
411 // multiple outputs right now).
412 std::vector<Effect *> output_effects;
413 for (unsigned i = 0; i < effects.size(); ++i) {
414 Effect *effect = effects[i];
415 if (outgoing_links.count(effect) == 0 || outgoing_links[effect].size() == 0) {
416 output_effects.push_back(effect);
419 assert(output_effects.size() == 1);
420 Effect *output_effect = output_effects[0];
422 // Add normalizers to get the output format right.
423 assert(output_gamma_curve.count(output_effect) != 0);
424 assert(output_color_space.count(output_effect) != 0);
425 ColorSpace current_color_space = output_color_space[output_effect];
426 if (current_color_space != output_format.color_space) {
427 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
428 colorspace_conversion->set_int("source_space", current_color_space);
429 colorspace_conversion->set_int("destination_space", output_format.color_space);
430 std::vector<Effect *> inputs;
431 inputs.push_back(output_effect);
432 colorspace_conversion->add_self_to_effect_chain(this, inputs);
433 output_color_space[colorspace_conversion] = output_format.color_space;
434 output_effect = colorspace_conversion;
436 GammaCurve current_gamma_curve = output_gamma_curve[output_effect];
437 if (current_gamma_curve != output_format.gamma_curve) {
438 if (current_gamma_curve != GAMMA_LINEAR) {
439 output_effect = normalize_to_linear_gamma(output_effect);
440 current_gamma_curve = GAMMA_LINEAR;
442 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
443 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
444 std::vector<Effect *> inputs;
445 inputs.push_back(output_effect);
446 gamma_conversion->add_self_to_effect_chain(this, inputs);
447 output_gamma_curve[gamma_conversion] = output_format.gamma_curve;
448 output_effect = gamma_conversion;
451 // Construct all needed GLSL programs, starting at the output.
452 construct_glsl_programs(output_effect);
454 // If we have more than one phase, we need intermediate render-to-texture.
455 // Construct an FBO, and then as many textures as we need.
456 // We choose the simplest option of having one texture per output,
457 // since otherwise this turns into an (albeit simple)
458 // register allocation problem.
459 if (phases.size() > 1) {
460 glGenFramebuffers(1, &fbo);
462 for (unsigned i = 0; i < phases.size() - 1; ++i) {
463 Effect *output_effect = phases[i].effects.back();
465 glGenTextures(1, &temp_texture);
467 glBindTexture(GL_TEXTURE_2D, temp_texture);
469 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
471 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
473 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
475 effect_output_textures.insert(std::make_pair(output_effect, temp_texture));
479 for (unsigned i = 0; i < inputs.size(); ++i) {
480 inputs[i]->finalize();
486 void EffectChain::render_to_screen()
493 glDisable(GL_DEPTH_TEST);
495 glDepthMask(GL_FALSE);
498 glMatrixMode(GL_PROJECTION);
500 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
502 glMatrixMode(GL_MODELVIEW);
505 if (phases.size() > 1) {
506 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
510 std::set<Effect *> generated_mipmaps;
511 for (unsigned i = 0; i < inputs.size(); ++i) {
512 // Inputs generate their own mipmaps if they need to
514 generated_mipmaps.insert(inputs[i]);
517 for (unsigned phase = 0; phase < phases.size(); ++phase) {
518 glUseProgram(phases[phase].glsl_program_num);
521 // Set up RTT inputs for this phase.
522 for (unsigned sampler = 0; sampler < phases[phase].inputs.size(); ++sampler) {
523 glActiveTexture(GL_TEXTURE0 + sampler);
524 Effect *input = phases[phase].inputs[sampler];
525 assert(effect_output_textures.count(input) != 0);
526 glBindTexture(GL_TEXTURE_2D, effect_output_textures[input]);
528 if (phases[phase].input_needs_mipmaps) {
529 if (generated_mipmaps.count(input) == 0) {
530 glGenerateMipmap(GL_TEXTURE_2D);
532 generated_mipmaps.insert(input);
534 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
537 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
541 assert(effect_ids.count(input));
542 std::string texture_name = std::string("tex_") + effect_ids[input];
543 glUniform1i(glGetUniformLocation(phases[phase].glsl_program_num, texture_name.c_str()), sampler);
547 // And now the output.
548 if (phase == phases.size() - 1) {
549 // Last phase goes directly to the screen.
550 glBindFramebuffer(GL_FRAMEBUFFER, 0);
553 Effect *last_effect = phases[phase].effects.back();
554 assert(effect_output_textures.count(last_effect) != 0);
555 glFramebufferTexture2D(
557 GL_COLOR_ATTACHMENT0,
559 effect_output_textures[last_effect],
564 // Give the required parameters to all the effects.
565 unsigned sampler_num = phases[phase].inputs.size();
566 for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
567 Effect *effect = phases[phase].effects[i];
568 effect->set_gl_state(phases[phase].glsl_program_num, effect_ids[effect], &sampler_num);
574 glTexCoord2f(0.0f, 0.0f);
575 glVertex2f(0.0f, 0.0f);
577 glTexCoord2f(1.0f, 0.0f);
578 glVertex2f(1.0f, 0.0f);
580 glTexCoord2f(1.0f, 1.0f);
581 glVertex2f(1.0f, 1.0f);
583 glTexCoord2f(0.0f, 1.0f);
584 glVertex2f(0.0f, 1.0f);
589 for (unsigned i = 0; i < phases[phase].effects.size(); ++i) {
590 Effect *effect = phases[phase].effects[i];
591 effect->clear_gl_state();