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