1 #ifndef _EFFECT_CHAIN_H
2 #define _EFFECT_CHAIN_H 1
8 #include "image_format.h"
14 // For internal use within Node.
22 // Whether you want pre- or postmultiplied alpha in the output
23 // (see effect.h for a discussion of pre- versus postmultiplied alpha).
24 enum OutputAlphaFormat {
25 OUTPUT_ALPHA_PREMULTIPLIED,
26 OUTPUT_ALPHA_POSTMULTIPLIED,
29 // A node in the graph; basically an effect and some associated information.
35 // Edges in the graph (forward and backward).
36 std::vector<Node *> outgoing_links;
37 std::vector<Node *> incoming_links;
40 // Identifier used to create unique variables in GLSL.
41 std::string effect_id;
43 // Logical size of the output of this effect, ie. the resolution
44 // you would get if you sampled it as a texture. If it is undefined
45 // (since the inputs differ in resolution), it will be 0x0.
46 // If both this and output_texture_{width,height} are set,
47 // they will be equal.
48 unsigned output_width, output_height;
50 // If output goes to RTT (otherwise, none of these are set).
51 // The Phase pointer is a but ugly; we should probably fix so
52 // that Phase takes other phases as inputs, instead of Node.
53 GLuint output_texture;
54 unsigned output_texture_width, output_texture_height;
57 // Used during the building of the effect chain.
58 Colorspace output_color_space;
59 GammaCurve output_gamma_curve;
60 AlphaType output_alpha_type;
62 friend class EffectChain;
65 // A rendering phase; a single GLSL program rendering a single quad.
67 GLint glsl_program_num, vertex_shader, fragment_shader;
68 bool input_needs_mipmaps;
70 // Inputs are only inputs from other phases (ie., those that come from RTT);
71 // input textures are not counted here.
72 std::vector<Node *> inputs;
74 std::vector<Node *> effects; // In order.
75 unsigned output_width, output_height;
80 EffectChain(float aspect_nom, float aspect_denom); // E.g., 16.0f, 9.0f for 16:9.
84 // input, effects, output, finalize need to come in that specific order.
86 // EffectChain takes ownership of the given input.
87 // input is returned back for convenience.
88 Input *add_input(Input *input);
90 // EffectChain takes ownership of the given effect.
91 // effect is returned back for convenience.
92 Effect *add_effect(Effect *effect) {
93 return add_effect(effect, last_added_effect());
95 Effect *add_effect(Effect *effect, Effect *input) {
96 std::vector<Effect *> inputs;
97 inputs.push_back(input);
98 return add_effect(effect, inputs);
100 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
101 std::vector<Effect *> inputs;
102 inputs.push_back(input1);
103 inputs.push_back(input2);
104 return add_effect(effect, inputs);
106 Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
108 void add_output(const ImageFormat &format, OutputAlphaFormat alpha_format);
110 // Set number of output bits, to scale the dither.
111 // 8 is the right value for most outputs.
112 // The default, 0, is a special value that means no dither.
113 void set_dither_bits(unsigned num_bits)
115 this->num_dither_bits = num_bits;
121 //void render(unsigned char *src, unsigned char *dst);
122 void render_to_screen()
124 render_to_fbo(0, 0, 0);
127 // Render the effect chain to the given FBO. If width=height=0, keeps
128 // the current viewport.
129 void render_to_fbo(GLuint fbo, unsigned width, unsigned height);
131 Effect *last_added_effect() {
135 return nodes.back()->effect;
139 // API for manipulating the graph directly. Intended to be used from
140 // effects and by EffectChain itself.
142 // Note that for nodes with multiple inputs, the order of calls to
143 // connect_nodes() will matter.
144 Node *add_node(Effect *effect);
145 void connect_nodes(Node *sender, Node *receiver);
146 void replace_receiver(Node *old_receiver, Node *new_receiver);
147 void replace_sender(Node *new_sender, Node *receiver);
148 void insert_node_between(Node *sender, Node *middle, Node *receiver);
151 // Fits a rectangle of the given size to the current aspect ratio
152 // (aspect_nom/aspect_denom) and returns the new width and height.
153 unsigned fit_rectangle_to_aspect(unsigned width, unsigned height);
155 // Compute the input sizes for all inputs for all effects in a given phase,
156 // and inform the effects about the results.
157 void inform_input_sizes(Phase *phase);
159 // Determine the preferred output size of a given phase.
160 // Requires that all input phases (if any) already have output sizes set.
161 void find_output_size(Phase *phase);
163 // Find all inputs eventually feeding into this effect that have
164 // output gamma different from GAMMA_LINEAR.
165 void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
167 // Create a GLSL program computing the given effects in order.
168 Phase *compile_glsl_program(const std::vector<Node *> &inputs,
169 const std::vector<Node *> &effects);
171 // Create all GLSL programs needed to compute the given effect, and all outputs
172 // that depends on it (whenever possible).
173 void construct_glsl_programs(Node *output);
175 // Output the current graph to the given file in a Graphviz-compatible format;
176 // only useful for debugging.
177 void output_dot(const char *filename);
178 std::vector<std::string> get_labels_for_edge(const Node *from, const Node *to);
179 void output_dot_edge(FILE *fp,
180 const std::string &from_node_id,
181 const std::string &to_node_id,
182 const std::vector<std::string> &labels);
184 // Some of the graph algorithms assume that the nodes array is sorted
185 // topologically (inputs are always before outputs), but some operations
186 // (like graph rewriting) can change that. This function restores that order.
187 void sort_all_nodes_topologically();
189 // Do the actual topological sort. <nodes> must be a connected, acyclic subgraph;
190 // links that go to nodes not in the set will be ignored.
191 std::vector<Node *> topological_sort(const std::vector<Node *> &nodes);
193 // Utility function used by topological_sort() to do a depth-first search.
194 // The reason why we store nodes left to visit instead of a more conventional
195 // list of nodes to visit is that we want to be able to limit ourselves to
196 // a subgraph instead of all nodes. The set thus serves a dual purpose.
197 void topological_sort_visit_node(Node *node, std::set<Node *> *nodes_left_to_visit, std::vector<Node *> *sorted_list);
199 // Used during finalize().
200 void find_color_spaces_for_inputs();
201 void propagate_alpha();
202 void propagate_gamma_and_color_space();
203 Node *find_output_node();
205 bool node_needs_colorspace_fix(Node *node);
206 void fix_internal_color_spaces();
207 void fix_output_color_space();
209 bool node_needs_alpha_fix(Node *node);
210 void fix_internal_alpha(unsigned step);
211 void fix_output_alpha();
213 bool node_needs_gamma_fix(Node *node);
214 void fix_internal_gamma_by_asking_inputs(unsigned step);
215 void fix_internal_gamma_by_inserting_nodes(unsigned step);
216 void fix_output_gamma();
217 void add_dither_if_needed();
219 float aspect_nom, aspect_denom;
220 ImageFormat output_format;
221 OutputAlphaFormat output_alpha_format;
223 std::vector<Node *> nodes;
224 std::map<Effect *, Node *> node_map;
225 Effect *dither_effect;
227 std::vector<Input *> inputs; // Also contained in nodes.
230 std::vector<Phase *> phases;
233 unsigned bytes_per_pixel, num_dither_bits;
237 #endif // !defined(_EFFECT_CHAIN_H)