]> git.sesse.net Git - movit/blob - effect_chain.h
Add a unit test for SaturationEffect.
[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 class Phase;
13
14 // A node in the graph; basically an effect and some associated information.
15 class Node {
16 public:
17         Effect *effect;
18         bool disabled;
19
20         // Edges in the graph (forward and backward).
21         std::vector<Node *> outgoing_links;
22         std::vector<Node *> incoming_links;
23
24 private:
25         // Identifier used to create unique variables in GLSL.
26         std::string effect_id;
27
28         // Logical size of the output of this effect, ie. the resolution
29         // you would get if you sampled it as a texture. If it is undefined
30         // (since the inputs differ in resolution), it will be 0x0.
31         // If both this and output_texture_{width,height} are set,
32         // they will be equal.
33         unsigned output_width, output_height;
34
35         // If output goes to RTT (otherwise, none of these are set).
36         // The Phase pointer is a but ugly; we should probably fix so
37         // that Phase takes other phases as inputs, instead of Node.
38         GLuint output_texture;
39         unsigned output_texture_width, output_texture_height;
40         Phase *phase;
41
42         // Used during the building of the effect chain.
43         ColorSpace output_color_space;
44         GammaCurve output_gamma_curve;
45
46         friend class EffectChain;
47 };
48
49 // A rendering phase; a single GLSL program rendering a single quad.
50 struct Phase {
51         GLint glsl_program_num;
52         bool input_needs_mipmaps;
53
54         // Inputs are only inputs from other phases (ie., those that come from RTT);
55         // input textures are not counted here.
56         std::vector<Node *> inputs;
57
58         std::vector<Node *> effects;  // In order.
59         unsigned output_width, output_height;
60 };
61
62 class EffectChain {
63 public:
64         EffectChain(float aspect_nom, float aspect_denom);  // E.g., 16.0f, 9.0f for 16:9.
65
66         // User API:
67         // input, effects, output, finalize need to come in that specific order.
68
69         // EffectChain takes ownership of the given input.
70         // input is returned back for convenience.
71         Input *add_input(Input *input);
72
73         // EffectChain takes ownership of the given effect.
74         // effect is returned back for convenience.
75         Effect *add_effect(Effect *effect) {
76                 return add_effect(effect, last_added_effect());
77         }
78         Effect *add_effect(Effect *effect, Effect *input) {
79                 std::vector<Effect *> inputs;
80                 inputs.push_back(input);
81                 return add_effect(effect, inputs);
82         }
83         Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
84                 std::vector<Effect *> inputs;
85                 inputs.push_back(input1);
86                 inputs.push_back(input2);
87                 return add_effect(effect, inputs);
88         }
89         Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
90
91         void add_output(const ImageFormat &format);
92         void finalize();
93
94         //void render(unsigned char *src, unsigned char *dst);
95         void render_to_screen()
96         {
97                 render_to_fbo(0, 0, 0);
98         }
99
100         // Render the effect chain to the given FBO. If width=height=0, keeps
101         // the current viewport.
102         void render_to_fbo(GLuint fbo, unsigned width, unsigned height);
103
104         Effect *last_added_effect() {
105                 if (nodes.empty()) {
106                         return NULL;
107                 } else {
108                         return nodes.back()->effect;
109                 }       
110         }
111
112         // API for manipulating the graph directly. Intended to be used from
113         // effects and by EffectChain itself.
114         //
115         // Note that for nodes with multiple inputs, the order of calls to
116         // connect_nodes() will matter.
117         Node *add_node(Effect *effect);
118         void connect_nodes(Node *sender, Node *receiver);
119         void replace_receiver(Node *old_receiver, Node *new_receiver);
120         void replace_sender(Node *new_sender, Node *receiver);
121         void insert_node_between(Node *sender, Node *middle, Node *receiver);
122
123 private:
124         // Fits a rectangle of the given size to the current aspect ratio
125         // (aspect_nom/aspect_denom) and returns the new width and height.
126         unsigned fit_rectangle_to_aspect(unsigned width, unsigned height);
127
128         // Compute the input sizes for all inputs for all effects in a given phase,
129         // and inform the effects about the results.    
130         void inform_input_sizes(Phase *phase);
131
132         // Determine the preferred output size of a given phase.
133         // Requires that all input phases (if any) already have output sizes set.
134         void find_output_size(Phase *phase);
135
136         // Find all inputs eventually feeding into this effect that have
137         // output gamma different from GAMMA_LINEAR.
138         void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
139
140         // Create a GLSL program computing the given effects in order.
141         Phase *compile_glsl_program(const std::vector<Node *> &inputs,
142                                     const std::vector<Node *> &effects);
143
144         // Create all GLSL programs needed to compute the given effect, and all outputs
145         // that depends on it (whenever possible).
146         void construct_glsl_programs(Node *output);
147
148         // Output the current graph to the given file in a Graphviz-compatible format;
149         // only useful for debugging.
150         void output_dot(const char *filename);
151
152         // Some of the graph algorithms assume that the nodes array is sorted
153         // topologically (inputs are always before outputs), but some operations
154         // (like graph rewriting) can change that. This function restores that order.
155         void sort_nodes_topologically();
156         void topological_sort_visit_node(Node *node, std::set<Node *> *visited_nodes, std::vector<Node *> *sorted_list);
157
158         // Used during finalize().
159         void propagate_gamma_and_color_space();
160         Node *find_output_node();
161
162         bool node_needs_colorspace_fix(Node *node);
163         void fix_internal_color_spaces();
164         void fix_output_color_space();
165
166         bool node_needs_gamma_fix(Node *node);
167         void fix_internal_gamma_by_asking_inputs(unsigned step);
168         void fix_internal_gamma_by_inserting_nodes(unsigned step);
169         void fix_output_gamma();
170
171         float aspect_nom, aspect_denom;
172         ImageFormat output_format;
173
174         std::vector<Node *> nodes;
175         std::map<Effect *, Node *> node_map;
176
177         std::vector<Input *> inputs;  // Also contained in nodes.
178
179         GLuint fbo;
180         std::vector<Phase *> phases;
181
182         GLenum format, bytes_per_pixel;
183         bool finalized;
184 };
185
186 #endif // !defined(_EFFECT_CHAIN_H)