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