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