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"
36 // For internal use within Node.
44 // Whether you want pre- or postmultiplied alpha in the output
45 // (see effect.h for a discussion of pre- versus postmultiplied alpha).
46 enum OutputAlphaFormat {
47 OUTPUT_ALPHA_FORMAT_PREMULTIPLIED,
48 OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED,
51 // A node in the graph; basically an effect and some associated information.
57 // Edges in the graph (forward and backward).
58 std::vector<Node *> outgoing_links;
59 std::vector<Node *> incoming_links;
62 // Logical size of the output of this effect, ie. the resolution
63 // you would get if you sampled it as a texture. If it is undefined
64 // (since the inputs differ in resolution), it will be 0x0.
65 // If both this and output_texture_{width,height} are set,
66 // they will be equal.
67 unsigned output_width, output_height;
69 // If output goes to RTT, which phase it is in (otherwise unset).
70 // This is a bit ugly; we should probably fix so that Phase takes other
71 // phases as inputs, instead of Node.
74 // If the effect has is_single_texture(), or if the output went to RTT
75 // and that texture has been bound to a sampler, the sampler number
76 // will be stored here.
78 // TODO: Can an RTT texture be used as inputs to multiple effects
79 // within the same phase? If so, we have a problem with modifying
80 // sampler state here.
81 int bound_sampler_num;
83 // Used during the building of the effect chain.
84 Colorspace output_color_space;
85 GammaCurve output_gamma_curve;
86 AlphaType output_alpha_type;
88 friend class EffectChain;
91 // A rendering phase; a single GLSL program rendering a single quad.
93 GLuint glsl_program_num; // Owned by the resource_pool.
94 bool input_needs_mipmaps;
96 // Inputs are only inputs from other phases (ie., those that come from RTT);
97 // input textures are not counted here.
98 std::vector<Node *> inputs;
100 std::vector<Node *> effects; // In order.
101 unsigned output_width, output_height, virtual_output_width, virtual_output_height;
103 // Identifier used to create unique variables in GLSL.
104 // Unique per-phase to increase cacheability of compiled shaders.
105 std::map<Node *, std::string> effect_ids;
110 // Aspect: e.g. 16.0f, 9.0f for 16:9.
111 // resource_pool is a pointer to a ResourcePool with which to share shaders
112 // and other resources (see resource_pool.h). If NULL (the default),
113 // will create its own that is not shared with anything else. Does not take
114 // ownership of the passed-in ResourcePool, but will naturally take ownership
115 // of its own internal one if created.
116 EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool = NULL);
120 // input, effects, output, finalize need to come in that specific order.
122 // EffectChain takes ownership of the given input.
123 // input is returned back for convenience.
124 Input *add_input(Input *input);
126 // EffectChain takes ownership of the given effect.
127 // effect is returned back for convenience.
128 Effect *add_effect(Effect *effect) {
129 return add_effect(effect, last_added_effect());
131 Effect *add_effect(Effect *effect, Effect *input) {
132 std::vector<Effect *> inputs;
133 inputs.push_back(input);
134 return add_effect(effect, inputs);
136 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
137 std::vector<Effect *> inputs;
138 inputs.push_back(input1);
139 inputs.push_back(input2);
140 return add_effect(effect, inputs);
142 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2, Effect *input3) {
143 std::vector<Effect *> inputs;
144 inputs.push_back(input1);
145 inputs.push_back(input2);
146 inputs.push_back(input3);
147 return add_effect(effect, inputs);
149 Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
151 void add_output(const ImageFormat &format, OutputAlphaFormat alpha_format);
153 // Set number of output bits, to scale the dither.
154 // 8 is the right value for most outputs.
155 // The default, 0, is a special value that means no dither.
156 void set_dither_bits(unsigned num_bits)
158 this->num_dither_bits = num_bits;
164 //void render(unsigned char *src, unsigned char *dst);
165 void render_to_screen()
167 render_to_fbo(0, 0, 0);
170 // Render the effect chain to the given FBO. If width=height=0, keeps
171 // the current viewport.
172 void render_to_fbo(GLuint fbo, unsigned width, unsigned height);
174 Effect *last_added_effect() {
178 return nodes.back()->effect;
182 // API for manipulating the graph directly. Intended to be used from
183 // effects and by EffectChain itself.
185 // Note that for nodes with multiple inputs, the order of calls to
186 // connect_nodes() will matter.
187 Node *add_node(Effect *effect);
188 void connect_nodes(Node *sender, Node *receiver);
189 void replace_receiver(Node *old_receiver, Node *new_receiver);
190 void replace_sender(Node *new_sender, Node *receiver);
191 void insert_node_between(Node *sender, Node *middle, Node *receiver);
192 Node *find_node_for_effect(Effect *effect) { return node_map[effect]; }
194 // Get the OpenGL sampler (GL_TEXTURE0, GL_TEXTURE1, etc.) for the
195 // input of the given node, so that one can modify the sampler state
196 // directly. Only valid to call during set_gl_state().
198 // Also, for this to be allowed, <node>'s effect must have
199 // needs_texture_bounce() set, so that it samples directly from a
200 // single-sampler input, or from an RTT texture.
201 GLenum get_input_sampler(Node *node, unsigned input_num) const;
203 // Get the current resource pool assigned to this EffectChain.
204 // Primarily to let effects allocate textures as needed.
205 // Any resources you get from the pool must be returned to the pool
206 // no later than in the Effect's destructor.
207 ResourcePool *get_resource_pool() { return resource_pool; }
210 // Make sure the output rectangle is at least large enough to hold
211 // the given input rectangle in both dimensions, and is of the
212 // current aspect ratio (aspect_nom/aspect_denom).
213 void size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height);
215 // Compute the input sizes for all inputs for all effects in a given phase,
216 // and inform the effects about the results.
217 void inform_input_sizes(Phase *phase);
219 // Determine the preferred output size of a given phase.
220 // Requires that all input phases (if any) already have output sizes set.
221 void find_output_size(Phase *phase);
223 // Find all inputs eventually feeding into this effect that have
224 // output gamma different from GAMMA_LINEAR.
225 void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
227 // Create a GLSL program computing the given effects in order.
228 Phase *compile_glsl_program(const std::vector<Node *> &inputs,
229 const std::vector<Node *> &effects);
231 // Create all GLSL programs needed to compute the given effect, and all outputs
232 // that depends on it (whenever possible).
233 void construct_glsl_programs(Node *output);
235 // Output the current graph to the given file in a Graphviz-compatible format;
236 // only useful for debugging.
237 void output_dot(const char *filename);
238 std::vector<std::string> get_labels_for_edge(const Node *from, const Node *to);
239 void output_dot_edge(FILE *fp,
240 const std::string &from_node_id,
241 const std::string &to_node_id,
242 const std::vector<std::string> &labels);
244 // Some of the graph algorithms assume that the nodes array is sorted
245 // topologically (inputs are always before outputs), but some operations
246 // (like graph rewriting) can change that. This function restores that order.
247 void sort_all_nodes_topologically();
249 // Do the actual topological sort. <nodes> must be a connected, acyclic subgraph;
250 // links that go to nodes not in the set will be ignored.
251 std::vector<Node *> topological_sort(const std::vector<Node *> &nodes);
253 // Utility function used by topological_sort() to do a depth-first search.
254 // The reason why we store nodes left to visit instead of a more conventional
255 // list of nodes to visit is that we want to be able to limit ourselves to
256 // a subgraph instead of all nodes. The set thus serves a dual purpose.
257 void topological_sort_visit_node(Node *node, std::set<Node *> *nodes_left_to_visit, std::vector<Node *> *sorted_list);
259 // Used during finalize().
260 void find_color_spaces_for_inputs();
261 void propagate_alpha();
262 void propagate_gamma_and_color_space();
263 Node *find_output_node();
265 bool node_needs_colorspace_fix(Node *node);
266 void fix_internal_color_spaces();
267 void fix_output_color_space();
269 bool node_needs_alpha_fix(Node *node);
270 void fix_internal_alpha(unsigned step);
271 void fix_output_alpha();
273 bool node_needs_gamma_fix(Node *node);
274 void fix_internal_gamma_by_asking_inputs(unsigned step);
275 void fix_internal_gamma_by_inserting_nodes(unsigned step);
276 void fix_output_gamma();
277 void add_dither_if_needed();
279 float aspect_nom, aspect_denom;
280 ImageFormat output_format;
281 OutputAlphaFormat output_alpha_format;
283 std::vector<Node *> nodes;
284 std::map<Effect *, Node *> node_map;
285 Effect *dither_effect;
287 std::vector<Input *> inputs; // Also contained in nodes.
288 std::vector<Phase *> phases;
290 unsigned num_dither_bits;
293 ResourcePool *resource_pool;
294 bool owns_resource_pool;
299 #endif // !defined(_MOVIT_EFFECT_CHAIN_H)