]> git.sesse.net Git - movit/blob - effect_chain.h
Add a file-level comment for EffectChain.
[movit] / effect_chain.h
1 #ifndef _MOVIT_EFFECT_CHAIN_H
2 #define _MOVIT_EFFECT_CHAIN_H 1
3
4 // An EffectChain is the largest basic entity in Movit; it contains everything
5 // needed to connects a series of effects, from inputs to outputs, and render
6 // them. Generally you set up your effect chain once and then call its render
7 // functions once per frame; setting one up can be relatively expensive,
8 // but rendering is fast.
9
10 #include <GL/glew.h>
11 #include <stdio.h>
12 #include <map>
13 #include <set>
14 #include <string>
15 #include <vector>
16
17 #include "image_format.h"
18
19 class Effect;
20 class Input;
21 struct Phase;
22
23 // For internal use within Node.
24 enum AlphaType {
25         ALPHA_INVALID = -1,
26         ALPHA_BLANK,
27         ALPHA_PREMULTIPLIED,
28         ALPHA_POSTMULTIPLIED,
29 };
30
31 // Whether you want pre- or postmultiplied alpha in the output
32 // (see effect.h for a discussion of pre- versus postmultiplied alpha).
33 enum OutputAlphaFormat {
34         OUTPUT_ALPHA_FORMAT_PREMULTIPLIED,
35         OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED,
36 };
37
38 // A node in the graph; basically an effect and some associated information.
39 class Node {
40 public:
41         Effect *effect;
42         bool disabled;
43
44         // Edges in the graph (forward and backward).
45         std::vector<Node *> outgoing_links;
46         std::vector<Node *> incoming_links;
47
48 private:
49         // Identifier used to create unique variables in GLSL.
50         std::string effect_id;
51
52         // Logical size of the output of this effect, ie. the resolution
53         // you would get if you sampled it as a texture. If it is undefined
54         // (since the inputs differ in resolution), it will be 0x0.
55         // If both this and output_texture_{width,height} are set,
56         // they will be equal.
57         unsigned output_width, output_height;
58
59         // If output goes to RTT (otherwise, none of these are set).
60         // The Phase pointer is a but ugly; we should probably fix so
61         // that Phase takes other phases as inputs, instead of Node.
62         GLuint output_texture;
63         unsigned output_texture_width, output_texture_height;
64         Phase *phase;
65
66         // Used during the building of the effect chain.
67         Colorspace output_color_space;
68         GammaCurve output_gamma_curve;
69         AlphaType output_alpha_type;
70
71         friend class EffectChain;
72 };
73
74 // A rendering phase; a single GLSL program rendering a single quad.
75 struct Phase {
76         GLint glsl_program_num, vertex_shader, fragment_shader;
77         bool input_needs_mipmaps;
78
79         // Inputs are only inputs from other phases (ie., those that come from RTT);
80         // input textures are not counted here.
81         std::vector<Node *> inputs;
82
83         std::vector<Node *> effects;  // In order.
84         unsigned output_width, output_height, virtual_output_width, virtual_output_height;
85 };
86
87 class EffectChain {
88 public:
89         EffectChain(float aspect_nom, float aspect_denom);  // E.g., 16.0f, 9.0f for 16:9.
90         ~EffectChain();
91
92         // User API:
93         // input, effects, output, finalize need to come in that specific order.
94
95         // EffectChain takes ownership of the given input.
96         // input is returned back for convenience.
97         Input *add_input(Input *input);
98
99         // EffectChain takes ownership of the given effect.
100         // effect is returned back for convenience.
101         Effect *add_effect(Effect *effect) {
102                 return add_effect(effect, last_added_effect());
103         }
104         Effect *add_effect(Effect *effect, Effect *input) {
105                 std::vector<Effect *> inputs;
106                 inputs.push_back(input);
107                 return add_effect(effect, inputs);
108         }
109         Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
110                 std::vector<Effect *> inputs;
111                 inputs.push_back(input1);
112                 inputs.push_back(input2);
113                 return add_effect(effect, inputs);
114         }
115         Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
116
117         void add_output(const ImageFormat &format, OutputAlphaFormat alpha_format);
118
119         // Set number of output bits, to scale the dither.
120         // 8 is the right value for most outputs.
121         // The default, 0, is a special value that means no dither.
122         void set_dither_bits(unsigned num_bits)
123         {
124                 this->num_dither_bits = num_bits;
125         }
126
127         void finalize();
128
129
130         //void render(unsigned char *src, unsigned char *dst);
131         void render_to_screen()
132         {
133                 render_to_fbo(0, 0, 0);
134         }
135
136         // Render the effect chain to the given FBO. If width=height=0, keeps
137         // the current viewport.
138         void render_to_fbo(GLuint fbo, unsigned width, unsigned height);
139
140         Effect *last_added_effect() {
141                 if (nodes.empty()) {
142                         return NULL;
143                 } else {
144                         return nodes.back()->effect;
145                 }       
146         }
147
148         // API for manipulating the graph directly. Intended to be used from
149         // effects and by EffectChain itself.
150         //
151         // Note that for nodes with multiple inputs, the order of calls to
152         // connect_nodes() will matter.
153         Node *add_node(Effect *effect);
154         void connect_nodes(Node *sender, Node *receiver);
155         void replace_receiver(Node *old_receiver, Node *new_receiver);
156         void replace_sender(Node *new_sender, Node *receiver);
157         void insert_node_between(Node *sender, Node *middle, Node *receiver);
158
159 private:
160         // Make sure the output rectangle is at least large enough to hold
161         // the given input rectangle in both dimensions, and is of the
162         // current aspect ratio (aspect_nom/aspect_denom).
163         void size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height);
164
165         // Compute the input sizes for all inputs for all effects in a given phase,
166         // and inform the effects about the results.    
167         void inform_input_sizes(Phase *phase);
168
169         // Determine the preferred output size of a given phase.
170         // Requires that all input phases (if any) already have output sizes set.
171         void find_output_size(Phase *phase);
172
173         // Find all inputs eventually feeding into this effect that have
174         // output gamma different from GAMMA_LINEAR.
175         void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
176
177         // Create a GLSL program computing the given effects in order.
178         Phase *compile_glsl_program(const std::vector<Node *> &inputs,
179                                     const std::vector<Node *> &effects);
180
181         // Create all GLSL programs needed to compute the given effect, and all outputs
182         // that depends on it (whenever possible).
183         void construct_glsl_programs(Node *output);
184
185         // Output the current graph to the given file in a Graphviz-compatible format;
186         // only useful for debugging.
187         void output_dot(const char *filename);
188         std::vector<std::string> get_labels_for_edge(const Node *from, const Node *to);
189         void output_dot_edge(FILE *fp,
190                              const std::string &from_node_id,
191                              const std::string &to_node_id,
192                              const std::vector<std::string> &labels);
193
194         // Some of the graph algorithms assume that the nodes array is sorted
195         // topologically (inputs are always before outputs), but some operations
196         // (like graph rewriting) can change that. This function restores that order.
197         void sort_all_nodes_topologically();
198
199         // Do the actual topological sort. <nodes> must be a connected, acyclic subgraph;
200         // links that go to nodes not in the set will be ignored.
201         std::vector<Node *> topological_sort(const std::vector<Node *> &nodes);
202
203         // Utility function used by topological_sort() to do a depth-first search.
204         // The reason why we store nodes left to visit instead of a more conventional
205         // list of nodes to visit is that we want to be able to limit ourselves to
206         // a subgraph instead of all nodes. The set thus serves a dual purpose.
207         void topological_sort_visit_node(Node *node, std::set<Node *> *nodes_left_to_visit, std::vector<Node *> *sorted_list);
208
209         // Used during finalize().
210         void find_color_spaces_for_inputs();
211         void propagate_alpha();
212         void propagate_gamma_and_color_space();
213         Node *find_output_node();
214
215         bool node_needs_colorspace_fix(Node *node);
216         void fix_internal_color_spaces();
217         void fix_output_color_space();
218
219         bool node_needs_alpha_fix(Node *node);
220         void fix_internal_alpha(unsigned step);
221         void fix_output_alpha();
222
223         bool node_needs_gamma_fix(Node *node);
224         void fix_internal_gamma_by_asking_inputs(unsigned step);
225         void fix_internal_gamma_by_inserting_nodes(unsigned step);
226         void fix_output_gamma();
227         void add_dither_if_needed();
228
229         float aspect_nom, aspect_denom;
230         ImageFormat output_format;
231         OutputAlphaFormat output_alpha_format;
232
233         std::vector<Node *> nodes;
234         std::map<Effect *, Node *> node_map;
235         Effect *dither_effect;
236
237         std::vector<Input *> inputs;  // Also contained in nodes.
238         std::vector<Phase *> phases;
239
240         unsigned num_dither_bits;
241         bool finalized;
242 };
243
244 #endif // !defined(_MOVIT_EFFECT_CHAIN_H)