1 #ifndef _EFFECT_CHAIN_H
2 #define _EFFECT_CHAIN_H 1
8 #include "image_format.h"
14 // A node in the graph; basically an effect and some associated information.
20 // Edges in the graph (forward and backward).
21 std::vector<Node *> outgoing_links;
22 std::vector<Node *> incoming_links;
25 // Identifier used to create unique variables in GLSL.
26 std::string effect_id;
28 // Logical size of the output of this effect, ie. the resolution
29 // you would get if you sampled it as a texture. If it is undefined
30 // (since the inputs differ in resolution), it will be 0x0.
31 // If both this and output_texture_{width,height} are set,
32 // they will be equal.
33 unsigned output_width, output_height;
35 // If output goes to RTT (otherwise, none of these are set).
36 // The Phase pointer is a but ugly; we should probably fix so
37 // that Phase takes other phases as inputs, instead of Node.
38 GLuint output_texture;
39 unsigned output_texture_width, output_texture_height;
42 // Used during the building of the effect chain.
43 Colorspace output_color_space;
44 GammaCurve output_gamma_curve;
46 friend class EffectChain;
49 // A rendering phase; a single GLSL program rendering a single quad.
51 GLint glsl_program_num, vertex_shader, fragment_shader;
52 bool input_needs_mipmaps;
54 // Inputs are only inputs from other phases (ie., those that come from RTT);
55 // input textures are not counted here.
56 std::vector<Node *> inputs;
58 std::vector<Node *> effects; // In order.
59 unsigned output_width, output_height;
64 EffectChain(float aspect_nom, float aspect_denom); // E.g., 16.0f, 9.0f for 16:9.
68 // input, effects, output, finalize need to come in that specific order.
70 // EffectChain takes ownership of the given input.
71 // input is returned back for convenience.
72 Input *add_input(Input *input);
74 // EffectChain takes ownership of the given effect.
75 // effect is returned back for convenience.
76 Effect *add_effect(Effect *effect) {
77 return add_effect(effect, last_added_effect());
79 Effect *add_effect(Effect *effect, Effect *input) {
80 std::vector<Effect *> inputs;
81 inputs.push_back(input);
82 return add_effect(effect, inputs);
84 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
85 std::vector<Effect *> inputs;
86 inputs.push_back(input1);
87 inputs.push_back(input2);
88 return add_effect(effect, inputs);
90 Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
92 void add_output(const ImageFormat &format);
94 // Set number of output bits, to scale the dither.
95 // 8 is the right value for most outputs.
96 // The default, 0, is a special value that means no dither.
97 void set_dither_bits(unsigned num_bits)
99 this->num_dither_bits = num_bits;
105 //void render(unsigned char *src, unsigned char *dst);
106 void render_to_screen()
108 render_to_fbo(0, 0, 0);
111 // Render the effect chain to the given FBO. If width=height=0, keeps
112 // the current viewport.
113 void render_to_fbo(GLuint fbo, unsigned width, unsigned height);
115 Effect *last_added_effect() {
119 return nodes.back()->effect;
123 // API for manipulating the graph directly. Intended to be used from
124 // effects and by EffectChain itself.
126 // Note that for nodes with multiple inputs, the order of calls to
127 // connect_nodes() will matter.
128 Node *add_node(Effect *effect);
129 void connect_nodes(Node *sender, Node *receiver);
130 void replace_receiver(Node *old_receiver, Node *new_receiver);
131 void replace_sender(Node *new_sender, Node *receiver);
132 void insert_node_between(Node *sender, Node *middle, Node *receiver);
135 // Fits a rectangle of the given size to the current aspect ratio
136 // (aspect_nom/aspect_denom) and returns the new width and height.
137 unsigned fit_rectangle_to_aspect(unsigned width, unsigned height);
139 // Compute the input sizes for all inputs for all effects in a given phase,
140 // and inform the effects about the results.
141 void inform_input_sizes(Phase *phase);
143 // Determine the preferred output size of a given phase.
144 // Requires that all input phases (if any) already have output sizes set.
145 void find_output_size(Phase *phase);
147 // Find all inputs eventually feeding into this effect that have
148 // output gamma different from GAMMA_LINEAR.
149 void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
151 // Create a GLSL program computing the given effects in order.
152 Phase *compile_glsl_program(const std::vector<Node *> &inputs,
153 const std::vector<Node *> &effects);
155 // Create all GLSL programs needed to compute the given effect, and all outputs
156 // that depends on it (whenever possible).
157 void construct_glsl_programs(Node *output);
159 // Output the current graph to the given file in a Graphviz-compatible format;
160 // only useful for debugging.
161 void output_dot(const char *filename);
163 // Some of the graph algorithms assume that the nodes array is sorted
164 // topologically (inputs are always before outputs), but some operations
165 // (like graph rewriting) can change that. This function restores that order.
166 void sort_nodes_topologically();
167 void topological_sort_visit_node(Node *node, std::set<Node *> *visited_nodes, std::vector<Node *> *sorted_list);
169 // Used during finalize().
170 void propagate_gamma_and_color_space();
171 Node *find_output_node();
173 bool node_needs_colorspace_fix(Node *node);
174 void fix_internal_color_spaces();
175 void fix_output_color_space();
177 bool node_needs_gamma_fix(Node *node);
178 void fix_internal_gamma_by_asking_inputs(unsigned step);
179 void fix_internal_gamma_by_inserting_nodes(unsigned step);
180 void fix_output_gamma();
181 void add_dither_if_needed();
183 float aspect_nom, aspect_denom;
184 ImageFormat output_format;
186 std::vector<Node *> nodes;
187 std::map<Effect *, Node *> node_map;
188 Effect *dither_effect;
190 std::vector<Input *> inputs; // Also contained in nodes.
193 std::vector<Phase *> phases;
196 unsigned bytes_per_pixel, num_dither_bits;
200 #endif // !defined(_EFFECT_CHAIN_H)