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
20 // Memory management (only relevant if you use multiple contexts):
21 // See corresponding comment in resource_pool.h. This holds even if you don't
22 // allocate your own ResourcePool, but let EffectChain hold its own.
31 #include "image_format.h"
40 // For internal use within Node.
48 // Whether you want pre- or postmultiplied alpha in the output
49 // (see effect.h for a discussion of pre- versus postmultiplied alpha).
50 enum OutputAlphaFormat {
51 OUTPUT_ALPHA_FORMAT_PREMULTIPLIED,
52 OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED,
55 // A node in the graph; basically an effect and some associated information.
61 // Edges in the graph (forward and backward).
62 std::vector<Node *> outgoing_links;
63 std::vector<Node *> incoming_links;
66 // Logical size of the output of this effect, ie. the resolution
67 // you would get if you sampled it as a texture. If it is undefined
68 // (since the inputs differ in resolution), it will be 0x0.
69 // If both this and output_texture_{width,height} are set,
70 // they will be equal.
71 unsigned output_width, output_height;
73 // If the effect has is_single_texture(), or if the output went to RTT
74 // and that texture has been bound to a sampler, the sampler number
75 // will be stored here.
77 // TODO: Can an RTT texture be used as inputs to multiple effects
78 // within the same phase? If so, we have a problem with modifying
79 // sampler state here.
80 int bound_sampler_num;
82 // Used during the building of the effect chain.
83 Colorspace output_color_space;
84 GammaCurve output_gamma_curve;
85 AlphaType output_alpha_type;
86 bool needs_mipmaps; // Directly or indirectly.
88 friend class EffectChain;
91 // A rendering phase; a single GLSL program rendering a single quad.
95 GLuint glsl_program_num; // Owned by the resource_pool.
96 bool input_needs_mipmaps;
98 // Inputs are only inputs from other phases (ie., those that come from RTT);
99 // input textures are counted as part of <effects>.
100 std::vector<Phase *> inputs;
101 std::vector<Node *> effects; // In order.
102 unsigned output_width, output_height, virtual_output_width, virtual_output_height;
104 // Identifier used to create unique variables in GLSL.
105 // Unique per-phase to increase cacheability of compiled shaders.
106 std::map<Node *, std::string> effect_ids;
108 // For measurement of GPU time used.
109 GLuint timer_query_object;
110 uint64_t time_elapsed_ns;
111 uint64_t num_measured_iterations;
116 // Aspect: e.g. 16.0f, 9.0f for 16:9.
117 // resource_pool is a pointer to a ResourcePool with which to share shaders
118 // and other resources (see resource_pool.h). If NULL (the default),
119 // will create its own that is not shared with anything else. Does not take
120 // ownership of the passed-in ResourcePool, but will naturally take ownership
121 // of its own internal one if created.
122 EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool = NULL);
126 // input, effects, output, finalize need to come in that specific order.
128 // EffectChain takes ownership of the given input.
129 // input is returned back for convenience.
130 Input *add_input(Input *input);
132 // EffectChain takes ownership of the given effect.
133 // effect is returned back for convenience.
134 Effect *add_effect(Effect *effect) {
135 return add_effect(effect, last_added_effect());
137 Effect *add_effect(Effect *effect, Effect *input) {
138 std::vector<Effect *> inputs;
139 inputs.push_back(input);
140 return add_effect(effect, inputs);
142 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
143 std::vector<Effect *> inputs;
144 inputs.push_back(input1);
145 inputs.push_back(input2);
146 return add_effect(effect, inputs);
148 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2, Effect *input3) {
149 std::vector<Effect *> inputs;
150 inputs.push_back(input1);
151 inputs.push_back(input2);
152 inputs.push_back(input3);
153 return add_effect(effect, inputs);
155 Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
157 void add_output(const ImageFormat &format, OutputAlphaFormat alpha_format);
159 // Set number of output bits, to scale the dither.
160 // 8 is the right value for most outputs.
161 // The default, 0, is a special value that means no dither.
162 void set_dither_bits(unsigned num_bits)
164 this->num_dither_bits = num_bits;
169 // Measure the GPU time used for each actual phase during rendering.
170 // Note that this is only available if GL_ARB_timer_query
171 // (or, equivalently, OpenGL 3.3) is available. Also note that measurement
172 // will incur a performance cost, as we wait for the measurements to
173 // complete at the end of rendering.
174 void enable_phase_timing(bool enable);
175 void reset_phase_timing();
176 void print_phase_timing();
178 //void render(unsigned char *src, unsigned char *dst);
179 void render_to_screen()
181 render_to_fbo(0, 0, 0);
184 // Render the effect chain to the given FBO. If width=height=0, keeps
185 // the current viewport.
186 void render_to_fbo(GLuint fbo, unsigned width, unsigned height);
188 Effect *last_added_effect() {
192 return nodes.back()->effect;
196 // API for manipulating the graph directly. Intended to be used from
197 // effects and by EffectChain itself.
199 // Note that for nodes with multiple inputs, the order of calls to
200 // connect_nodes() will matter.
201 Node *add_node(Effect *effect);
202 void connect_nodes(Node *sender, Node *receiver);
203 void replace_receiver(Node *old_receiver, Node *new_receiver);
204 void replace_sender(Node *new_sender, Node *receiver);
205 void insert_node_between(Node *sender, Node *middle, Node *receiver);
206 Node *find_node_for_effect(Effect *effect) { return node_map[effect]; }
208 // Get the OpenGL sampler (GL_TEXTURE0, GL_TEXTURE1, etc.) for the
209 // input of the given node, so that one can modify the sampler state
210 // directly. Only valid to call during set_gl_state().
212 // Also, for this to be allowed, <node>'s effect must have
213 // needs_texture_bounce() set, so that it samples directly from a
214 // single-sampler input, or from an RTT texture.
215 GLenum get_input_sampler(Node *node, unsigned input_num) const;
217 // Get the current resource pool assigned to this EffectChain.
218 // Primarily to let effects allocate textures as needed.
219 // Any resources you get from the pool must be returned to the pool
220 // no later than in the Effect's destructor.
221 ResourcePool *get_resource_pool() { return resource_pool; }
224 // Make sure the output rectangle is at least large enough to hold
225 // the given input rectangle in both dimensions, and is of the
226 // current aspect ratio (aspect_nom/aspect_denom).
227 void size_rectangle_to_fit(unsigned width, unsigned height, unsigned *output_width, unsigned *output_height);
229 // Compute the input sizes for all inputs for all effects in a given phase,
230 // and inform the effects about the results.
231 void inform_input_sizes(Phase *phase);
233 // Determine the preferred output size of a given phase.
234 // Requires that all input phases (if any) already have output sizes set.
235 void find_output_size(Phase *phase);
237 // Find all inputs eventually feeding into this effect that have
238 // output gamma different from GAMMA_LINEAR.
239 void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
241 // Create a GLSL program computing the effects for this phase in order.
242 void compile_glsl_program(Phase *phase);
244 // Create all GLSL programs needed to compute the given effect, and all outputs
245 // that depend on it (whenever possible). Returns the phase that has <output>
246 // as the last effect. Also pushes all phases in order onto <phases>.
247 Phase *construct_phase(Node *output, std::map<Node *, Phase *> *completed_effects);
249 // Execute one phase, ie. set up all inputs, effects and outputs, and render the quad.
250 void execute_phase(Phase *phase, bool last_phase, std::map<Phase *, GLuint> *output_textures, std::set<Phase *> *generated_mipmaps);
252 // Set up the given sampler number for sampling from an RTT texture,
253 // and bind it to "tex_" plus the given GLSL variable.
254 void setup_rtt_sampler(GLuint glsl_program_num, int sampler_num, const std::string &effect_id, bool use_mipmaps);
256 // Output the current graph to the given file in a Graphviz-compatible format;
257 // only useful for debugging.
258 void output_dot(const char *filename);
259 std::vector<std::string> get_labels_for_edge(const Node *from, const Node *to);
260 void output_dot_edge(FILE *fp,
261 const std::string &from_node_id,
262 const std::string &to_node_id,
263 const std::vector<std::string> &labels);
265 // Some of the graph algorithms assume that the nodes array is sorted
266 // topologically (inputs are always before outputs), but some operations
267 // (like graph rewriting) can change that. This function restores that order.
268 void sort_all_nodes_topologically();
270 // Do the actual topological sort. <nodes> must be a connected, acyclic subgraph;
271 // links that go to nodes not in the set will be ignored.
272 std::vector<Node *> topological_sort(const std::vector<Node *> &nodes);
274 // Utility function used by topological_sort() to do a depth-first search.
275 // The reason why we store nodes left to visit instead of a more conventional
276 // list of nodes to visit is that we want to be able to limit ourselves to
277 // a subgraph instead of all nodes. The set thus serves a dual purpose.
278 void topological_sort_visit_node(Node *node, std::set<Node *> *nodes_left_to_visit, std::vector<Node *> *sorted_list);
280 // Used during finalize().
281 void find_color_spaces_for_inputs();
282 void propagate_alpha();
283 void propagate_gamma_and_color_space();
284 Node *find_output_node();
286 bool node_needs_colorspace_fix(Node *node);
287 void fix_internal_color_spaces();
288 void fix_output_color_space();
290 bool node_needs_alpha_fix(Node *node);
291 void fix_internal_alpha(unsigned step);
292 void fix_output_alpha();
294 bool node_needs_gamma_fix(Node *node);
295 void fix_internal_gamma_by_asking_inputs(unsigned step);
296 void fix_internal_gamma_by_inserting_nodes(unsigned step);
297 void fix_output_gamma();
298 void add_dither_if_needed();
300 float aspect_nom, aspect_denom;
301 ImageFormat output_format;
302 OutputAlphaFormat output_alpha_format;
304 std::vector<Node *> nodes;
305 std::map<Effect *, Node *> node_map;
306 Effect *dither_effect;
308 std::vector<Input *> inputs; // Also contained in nodes.
309 std::vector<Phase *> phases;
311 unsigned num_dither_bits;
314 ResourcePool *resource_pool;
315 bool owns_resource_pool;
317 bool do_phase_timing;
322 #endif // !defined(_MOVIT_EFFECT_CHAIN_H)