]> git.sesse.net Git - movit/blob - effect_chain.h
Typo fix.
[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 class Phase;
13
14 // A node in the graph; basically an effect and some associated information.
15 class Node {
16 public:
17         Effect *effect;
18         bool disabled;
19
20         // Edges in the graph (forward and backward).
21         std::vector<Node *> outgoing_links;
22         std::vector<Node *> incoming_links;
23
24 private:
25         // Identifier used to create unique variables in GLSL.
26         std::string effect_id;
27
28         // If output goes to RTT (otherwise, none of these are set).
29         // The Phase pointer is a but ugly; we should probably fix so
30         // that Phase takes other phases as inputs, instead of Node.
31         GLuint output_texture;
32         unsigned output_texture_width, output_texture_height;
33         Phase *phase;
34
35         // Used during the building of the effect chain.
36         ColorSpace output_color_space;
37         GammaCurve output_gamma_curve;
38
39         friend class EffectChain;
40 };
41
42 // A rendering phase; a single GLSL program rendering a single quad.
43 struct Phase {
44         GLint glsl_program_num;
45         bool input_needs_mipmaps;
46
47         // Inputs are only inputs from other phases (ie., those that come from RTT);
48         // input textures are not counted here.
49         std::vector<Node *> inputs;
50
51         std::vector<Node *> effects;  // In order.
52         unsigned output_width, output_height;
53 };
54
55 class EffectChain {
56 public:
57         EffectChain(float aspect_nom, float aspect_denom);  // E.g., 16.0f, 9.0f for 16:9.
58
59         // User API:
60         // input, effects, output, finalize need to come in that specific order.
61
62         // EffectChain takes ownership of the given input.
63         // input is returned back for convenience.
64         Input *add_input(Input *input);
65
66         // EffectChain takes ownership of the given effect.
67         // effect is returned back for convenience.
68         Effect *add_effect(Effect *effect) {
69                 return add_effect(effect, last_added_effect());
70         }
71         Effect *add_effect(Effect *effect, Effect *input) {
72                 std::vector<Effect *> inputs;
73                 inputs.push_back(input);
74                 return add_effect(effect, inputs);
75         }
76         Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
77                 std::vector<Effect *> inputs;
78                 inputs.push_back(input1);
79                 inputs.push_back(input2);
80                 return add_effect(effect, inputs);
81         }
82         Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
83
84         void add_output(const ImageFormat &format);
85         void finalize();
86
87         //void render(unsigned char *src, unsigned char *dst);
88         void render_to_screen();
89
90         Effect *last_added_effect() {
91                 if (nodes.empty()) {
92                         return NULL;
93                 } else {
94                         return nodes.back()->effect;
95                 }       
96         }
97
98         // API for manipulating the graph directly. Intended to be used from
99         // effects and by EffectChain itself.
100         //
101         // Note that for nodes with multiple inputs, the order of calls to
102         // connect_nodes() will matter.
103         Node *add_node(Effect *effect);
104         void connect_nodes(Node *sender, Node *receiver);
105         void replace_receiver(Node *old_receiver, Node *new_receiver);
106         void replace_sender(Node *new_sender, Node *receiver);
107         void insert_node_between(Node *sender, Node *middle, Node *receiver);
108
109 private:
110         // Fits a rectangle of the given size to the current aspect ratio
111         // (aspect_nom/aspect_denom) and returns the new width and height.
112         unsigned fit_rectangle_to_aspect(unsigned width, unsigned height);
113
114         // Determine the preferred output size of a given phase.
115         // Requires that all input phases (if any) already have output sizes set.
116         void find_output_size(Phase *phase);
117
118         // Find all inputs eventually feeding into this effect that have
119         // output gamma different from GAMMA_LINEAR.
120         void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
121
122         // Create a GLSL program computing the given effects in order.
123         Phase *compile_glsl_program(const std::vector<Node *> &inputs,
124                                     const std::vector<Node *> &effects);
125
126         // Create all GLSL programs needed to compute the given effect, and all outputs
127         // that depends on it (whenever possible).
128         void construct_glsl_programs(Node *output);
129
130         // Output the current graph to the given file in a Graphviz-compatible format;
131         // only useful for debugging.
132         void output_dot(const char *filename);
133
134         // Some of the graph algorithms assume that the nodes array is sorted
135         // topologically (inputs are always before outputs), but some operations
136         // (like graph rewriting) can change that. This function restores that order.
137         void sort_nodes_topologically();
138         void topological_sort_visit_node(Node *node, std::set<Node *> *visited_nodes, std::vector<Node *> *sorted_list);
139
140         // Used during finalize().
141         void propagate_gamma_and_color_space();
142         Node *find_output_node();
143
144         bool node_needs_colorspace_fix(Node *node);
145         void fix_internal_color_spaces();
146         void fix_output_color_space();
147
148         bool node_needs_gamma_fix(Node *node);
149         void fix_internal_gamma_by_asking_inputs(unsigned step);
150         void fix_internal_gamma_by_inserting_nodes(unsigned step);
151         void fix_output_gamma();
152
153         float aspect_nom, aspect_denom;
154         ImageFormat output_format;
155
156         std::vector<Node *> nodes;
157         std::map<Effect *, Node *> node_map;
158
159         std::vector<Input *> inputs;  // Also contained in nodes.
160
161         GLuint fbo;
162         std::vector<Phase *> phases;
163
164         GLenum format, bytes_per_pixel;
165         bool finalized;
166 };
167
168 #endif // !defined(_EFFECT_CHAIN_H)