]> git.sesse.net Git - movit/blob - effect_chain.h
67955786c9e2efd9a3bfed69021f3d7d4efb05a2
[movit] / effect_chain.h
1 #ifndef _EFFECT_CHAIN_H
2 #define _EFFECT_CHAIN_H 1
3
4 #include <set>
5 #include <vector>
6
7 #include "effect.h"
8 #include "image_format.h"
9 #include "input.h"
10
11 class EffectChain {
12 public:
13         EffectChain(unsigned width, unsigned height);
14
15         // User API:
16         // input, effects, output, finalize need to come in that specific order.
17
18         // EffectChain takes ownership of the given input.
19         // input is returned back for convenience.
20         Input *add_input(Input *input);
21
22         // EffectChain takes ownership of the given effect.
23         // effect is returned back for convenience.
24         Effect *add_effect(Effect *effect) {
25                 return add_effect(effect, last_added_effect());
26         }
27         Effect *add_effect(Effect *effect, Effect *input) {
28                 std::vector<Effect *> inputs;
29                 inputs.push_back(input);
30                 return add_effect(effect, inputs);
31         }
32         Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
33                 std::vector<Effect *> inputs;
34                 inputs.push_back(input1);
35                 inputs.push_back(input2);
36                 return add_effect(effect, inputs);
37         }
38         Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
39
40         // Similar to add_effect, but:
41         //
42         //  * Does not insert any normalizing effects.
43         //  * Does not ask the effect to insert itself, so it won't work
44         //    with meta-effects.
45         //
46         // We should really separate out these two “sides” of Effect in the
47         // type system soon.
48         void add_effect_raw(Effect *effect, const std::vector<Effect *> &inputs);
49
50         void add_output(const ImageFormat &format);
51         void finalize();
52
53         //void render(unsigned char *src, unsigned char *dst);
54         void render_to_screen();
55
56         Effect *last_added_effect() {
57                 if (effects.empty()) {
58                         return NULL;
59                 } else {
60                         return effects.back();
61                 }       
62         }
63
64 private:
65         struct Phase {
66                 GLint glsl_program_num;
67                 bool input_needs_mipmaps;
68                 std::vector<Effect *> inputs;   // Only from other phases; input textures are not counted here.
69                 std::vector<Effect *> effects;  // In order.
70         };
71
72         void find_all_nonlinear_inputs(Effect *effect,
73                                        std::vector<Input *> *nonlinear_inputs,
74                                        std::vector<Effect *> *intermediates);
75         Effect *normalize_to_linear_gamma(Effect *input);
76         Effect *normalize_to_srgb(Effect *input);
77
78         void draw_vertex(float x, float y, const std::vector<Effect *> &inputs);
79
80         // Create a GLSL program computing the given effects in order.
81         Phase compile_glsl_program(const std::vector<Effect *> &inputs, const std::vector<Effect *> &effects);
82
83         // Create all GLSL programs needed to compute the given effect, and all outputs
84         // that depends on it (whenever possible).
85         void construct_glsl_programs(Effect *output);
86
87         unsigned width, height;
88         ImageFormat output_format;
89         std::vector<Effect *> effects;
90         std::vector<Input *> inputs;  // Also contained in effects.
91         std::map<Effect *, std::string> effect_ids;
92         std::map<Effect *, GLuint> effect_output_textures;
93         std::map<Effect *, std::vector<Effect *> > outgoing_links;
94         std::map<Effect *, std::vector<Effect *> > incoming_links;
95
96         GLuint fbo;
97         std::vector<Phase> phases;
98
99         GLenum format, bytes_per_pixel;
100         bool finalized;
101
102         // Used during the building of the effect chain.
103         std::map<Effect *, ColorSpace> output_color_space;
104         std::map<Effect *, GammaCurve> output_gamma_curve;      
105 };
106
107
108 #endif // !defined(_EFFECT_CHAIN_H)