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