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