1 #ifndef _MOVIT_EFFECT_CHAIN_H
2 #define _MOVIT_EFFECT_CHAIN_H 1
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.
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
27 #include "image_format.h"
34 // For internal use within Node.
42 // Whether you want pre- or postmultiplied alpha in the output
43 // (see effect.h for a discussion of pre- versus postmultiplied alpha).
44 enum OutputAlphaFormat {
45 OUTPUT_ALPHA_FORMAT_PREMULTIPLIED,
46 OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED,
49 // A node in the graph; basically an effect and some associated information.
55 // Edges in the graph (forward and backward).
56 std::vector<Node *> outgoing_links;
57 std::vector<Node *> incoming_links;
60 // Logical size of the output of this effect, ie. the resolution
61 // you would get if you sampled it as a texture. If it is undefined
62 // (since the inputs differ in resolution), it will be 0x0.
63 // If both this and output_texture_{width,height} are set,
64 // they will be equal.
65 unsigned output_width, output_height;
67 // If output goes to RTT, which phase it is in (otherwise unset).
68 // This is a bit ugly; we should probably fix so that Phase takes other
69 // phases as inputs, instead of Node.
72 // Used during the building of the effect chain.
73 Colorspace output_color_space;
74 GammaCurve output_gamma_curve;
75 AlphaType output_alpha_type;
77 friend class EffectChain;
80 // A rendering phase; a single GLSL program rendering a single quad.
82 GLuint glsl_program_num; // Owned by the resource_pool.
83 bool input_needs_mipmaps;
85 // Inputs are only inputs from other phases (ie., those that come from RTT);
86 // input textures are not counted here.
87 std::vector<Node *> inputs;
89 std::vector<Node *> effects; // In order.
90 unsigned output_width, output_height, virtual_output_width, virtual_output_height;
92 // Identifier used to create unique variables in GLSL.
93 // Unique per-phase to increase cacheability of compiled shaders.
94 std::map<Node *, std::string> effect_ids;
99 // Aspect: e.g. 16.0f, 9.0f for 16:9.
100 // resource_pool is a pointer to a ResourcePool with which to share shaders
101 // and other resources (see resource_pool.h). If NULL (the default),
102 // will create its own that is not shared with anything else. Does not take
103 // ownership of the passed-in ResourcePool, but will naturally take ownership
104 // of its own internal one if created.
105 EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool = NULL);
109 // input, effects, output, finalize need to come in that specific order.
111 // EffectChain takes ownership of the given input.
112 // input is returned back for convenience.
113 Input *add_input(Input *input);
115 // EffectChain takes ownership of the given effect.
116 // effect is returned back for convenience.
117 Effect *add_effect(Effect *effect) {
118 return add_effect(effect, last_added_effect());
120 Effect *add_effect(Effect *effect, Effect *input) {
121 std::vector<Effect *> inputs;
122 inputs.push_back(input);
123 return add_effect(effect, inputs);
125 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
126 std::vector<Effect *> inputs;
127 inputs.push_back(input1);
128 inputs.push_back(input2);
129 return add_effect(effect, inputs);
131 Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
133 void add_output(const ImageFormat &format, OutputAlphaFormat alpha_format);
135 // Set number of output bits, to scale the dither.
136 // 8 is the right value for most outputs.
137 // The default, 0, is a special value that means no dither.
138 void set_dither_bits(unsigned num_bits)
140 this->num_dither_bits = num_bits;
146 //void render(unsigned char *src, unsigned char *dst);
147 void render_to_screen()
149 render_to_fbo(0, 0, 0);
152 // Render the effect chain to the given FBO. If width=height=0, keeps
153 // the current viewport.
154 void render_to_fbo(GLuint fbo, unsigned width, unsigned height);
156 Effect *last_added_effect() {
160 return nodes.back()->effect;
164 // API for manipulating the graph directly. Intended to be used from
165 // effects and by EffectChain itself.
167 // Note that for nodes with multiple inputs, the order of calls to
168 // connect_nodes() will matter.
169 Node *add_node(Effect *effect);
170 void connect_nodes(Node *sender, Node *receiver);
171 void replace_receiver(Node *old_receiver, Node *new_receiver);
172 void replace_sender(Node *new_sender, Node *receiver);
173 void insert_node_between(Node *sender, Node *middle, Node *receiver);
175 // Get the current resource pool assigned to this EffectChain.
176 // Primarily to let effects allocate textures as needed.
177 // Any resources you get from the pool must be returned to the pool
178 // no later than in the Effect's destructor.
179 ResourcePool *get_resource_pool() { return resource_pool; }
182 // Make sure the output rectangle is at least large enough to hold
183 // the given input rectangle in both dimensions, and is of the
184 // current aspect ratio (aspect_nom/aspect_denom).
185 void size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height);
187 // Compute the input sizes for all inputs for all effects in a given phase,
188 // and inform the effects about the results.
189 void inform_input_sizes(Phase *phase);
191 // Determine the preferred output size of a given phase.
192 // Requires that all input phases (if any) already have output sizes set.
193 void find_output_size(Phase *phase);
195 // Find all inputs eventually feeding into this effect that have
196 // output gamma different from GAMMA_LINEAR.
197 void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
199 // Create a GLSL program computing the given effects in order.
200 Phase *compile_glsl_program(const std::vector<Node *> &inputs,
201 const std::vector<Node *> &effects);
203 // Create all GLSL programs needed to compute the given effect, and all outputs
204 // that depends on it (whenever possible).
205 void construct_glsl_programs(Node *output);
207 // Output the current graph to the given file in a Graphviz-compatible format;
208 // only useful for debugging.
209 void output_dot(const char *filename);
210 std::vector<std::string> get_labels_for_edge(const Node *from, const Node *to);
211 void output_dot_edge(FILE *fp,
212 const std::string &from_node_id,
213 const std::string &to_node_id,
214 const std::vector<std::string> &labels);
216 // Some of the graph algorithms assume that the nodes array is sorted
217 // topologically (inputs are always before outputs), but some operations
218 // (like graph rewriting) can change that. This function restores that order.
219 void sort_all_nodes_topologically();
221 // Do the actual topological sort. <nodes> must be a connected, acyclic subgraph;
222 // links that go to nodes not in the set will be ignored.
223 std::vector<Node *> topological_sort(const std::vector<Node *> &nodes);
225 // Utility function used by topological_sort() to do a depth-first search.
226 // The reason why we store nodes left to visit instead of a more conventional
227 // list of nodes to visit is that we want to be able to limit ourselves to
228 // a subgraph instead of all nodes. The set thus serves a dual purpose.
229 void topological_sort_visit_node(Node *node, std::set<Node *> *nodes_left_to_visit, std::vector<Node *> *sorted_list);
231 // Used during finalize().
232 void find_color_spaces_for_inputs();
233 void propagate_alpha();
234 void propagate_gamma_and_color_space();
235 Node *find_output_node();
237 bool node_needs_colorspace_fix(Node *node);
238 void fix_internal_color_spaces();
239 void fix_output_color_space();
241 bool node_needs_alpha_fix(Node *node);
242 void fix_internal_alpha(unsigned step);
243 void fix_output_alpha();
245 bool node_needs_gamma_fix(Node *node);
246 void fix_internal_gamma_by_asking_inputs(unsigned step);
247 void fix_internal_gamma_by_inserting_nodes(unsigned step);
248 void fix_output_gamma();
249 void add_dither_if_needed();
251 float aspect_nom, aspect_denom;
252 ImageFormat output_format;
253 OutputAlphaFormat output_alpha_format;
255 std::vector<Node *> nodes;
256 std::map<Effect *, Node *> node_map;
257 Effect *dither_effect;
259 std::vector<Input *> inputs; // Also contained in nodes.
260 std::vector<Phase *> phases;
262 unsigned num_dither_bits;
265 ResourcePool *resource_pool;
266 bool owns_resource_pool;
269 #endif // !defined(_MOVIT_EFFECT_CHAIN_H)