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