]> git.sesse.net Git - movit/blob - effect_chain.h
We need to link to -lrt, since we use the clock functions from there.
[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 (nodes.empty()) {
58                         return NULL;
59                 } else {
60                         return nodes.back()->effect;
61                 }       
62         }
63
64 private:
65         struct Phase;
66
67         // A node in the graph; basically an effect and some associated information.
68         struct Node {
69                 Effect *effect;
70
71                 // Identifier used to create unique variables in GLSL.
72                 std::string effect_id;
73
74                 // Edges in the graph (forward and backward).
75                 std::vector<Node *> outgoing_links;
76                 std::vector<Node *> incoming_links;
77
78                 // If output goes to RTT (otherwise, none of these are set).
79                 // The Phsae pointer is a but ugly; we should probably fix so
80                 // that Phase takes other phases as inputs, instead of Node.
81                 GLuint output_texture;
82                 unsigned output_texture_width, output_texture_height;
83                 Phase *phase;
84
85                 // Used during the building of the effect chain.
86                 ColorSpace output_color_space;
87                 GammaCurve output_gamma_curve;  
88         };
89
90         // A rendering phase; a single GLSL program rendering a single quad.
91         struct Phase {
92                 GLint glsl_program_num;
93                 bool input_needs_mipmaps;
94
95                 // Inputs are only inputs from other phases (ie., those that come from RTT);
96                 // input textures are not counted here.
97                 std::vector<Node *> inputs;
98
99                 std::vector<Node *> effects;  // In order.
100                 unsigned output_width, output_height;
101         };
102
103         // Determine the preferred output size of a given phase.
104         // Requires that all input phases (if any) already have output sizes set.
105         void find_output_size(Phase *phase);
106
107         void find_all_nonlinear_inputs(Node *effect,
108                                        std::vector<Node *> *nonlinear_inputs,
109                                        std::vector<Node *> *intermediates);
110         Node *normalize_to_linear_gamma(Node *input);
111         Node *normalize_to_srgb(Node *input);
112
113         void draw_vertex(float x, float y, const std::vector<Effect *> &inputs);
114
115         // Create a GLSL program computing the given effects in order.
116         Phase *compile_glsl_program(const std::vector<Node *> &inputs,
117                                     const std::vector<Node *> &effects);
118
119         // Create all GLSL programs needed to compute the given effect, and all outputs
120         // that depends on it (whenever possible).
121         void construct_glsl_programs(Node *output);
122
123         unsigned width, height;
124         ImageFormat output_format;
125
126         std::vector<Node *> nodes;
127         std::map<Effect *, Node *> node_map;
128
129         std::vector<Input *> inputs;  // Also contained in nodes.
130
131         GLuint fbo;
132         std::vector<Phase *> phases;
133
134         GLenum format, bytes_per_pixel;
135         bool finalized;
136 };
137
138
139 #endif // !defined(_EFFECT_CHAIN_H)