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 // If output goes to RTT (otherwise, none of these are set).
29 // The Phsae pointer is a but ugly; we should probably fix so
30 // that Phase takes other phases as inputs, instead of Node.
31 GLuint output_texture;
32 unsigned output_texture_width, output_texture_height;
35 // Used during the building of the effect chain.
36 ColorSpace output_color_space;
37 GammaCurve output_gamma_curve;
39 friend class EffectChain;
42 // A rendering phase; a single GLSL program rendering a single quad.
44 GLint glsl_program_num;
45 bool input_needs_mipmaps;
47 // Inputs are only inputs from other phases (ie., those that come from RTT);
48 // input textures are not counted here.
49 std::vector<Node *> inputs;
51 std::vector<Node *> effects; // In order.
52 unsigned output_width, output_height;
57 EffectChain(unsigned width, unsigned height);
60 // input, effects, output, finalize need to come in that specific order.
62 // EffectChain takes ownership of the given input.
63 // input is returned back for convenience.
64 Input *add_input(Input *input);
66 // EffectChain takes ownership of the given effect.
67 // effect is returned back for convenience.
68 Effect *add_effect(Effect *effect) {
69 return add_effect(effect, last_added_effect());
71 Effect *add_effect(Effect *effect, Effect *input) {
72 std::vector<Effect *> inputs;
73 inputs.push_back(input);
74 return add_effect(effect, inputs);
76 Effect *add_effect(Effect *effect, Effect *input1, Effect *input2) {
77 std::vector<Effect *> inputs;
78 inputs.push_back(input1);
79 inputs.push_back(input2);
80 return add_effect(effect, inputs);
82 Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
84 void add_output(const ImageFormat &format);
87 //void render(unsigned char *src, unsigned char *dst);
88 void render_to_screen();
90 Effect *last_added_effect() {
94 return nodes.back()->effect;
98 // API for manipulating the graph directly. Intended to be used from
99 // effects and by EffectChain itself.
101 // Note that for nodes with multiple inputs, the order of calls to
102 // connect_nodes() will matter.
103 Node *add_node(Effect *effect);
104 void connect_nodes(Node *sender, Node *receiver);
105 void replace_receiver(Node *old_receiver, Node *new_receiver);
106 void replace_sender(Node *new_sender, Node *receiver);
107 void insert_node_between(Node *sender, Node *middle, Node *receiver);
110 // Determine the preferred output size of a given phase.
111 // Requires that all input phases (if any) already have output sizes set.
112 void find_output_size(Phase *phase);
114 // Find all inputs eventually feeding into this effect that have
115 // output gamma different from GAMMA_LINEAR.
116 void find_all_nonlinear_inputs(Node *effect, std::vector<Node *> *nonlinear_inputs);
118 // Create a GLSL program computing the given effects in order.
119 Phase *compile_glsl_program(const std::vector<Node *> &inputs,
120 const std::vector<Node *> &effects);
122 // Create all GLSL programs needed to compute the given effect, and all outputs
123 // that depends on it (whenever possible).
124 void construct_glsl_programs(Node *output);
126 // Output the current graph to the given file in a Graphviz-compatible format;
127 // only useful for debugging.
128 void output_dot(const char *filename);
130 // Some of the graph algorithms assume that the nodes array is sorted
131 // topologically (inputs are always before outputs), but some operations
132 // (like graph rewriting) can change that. This function restores that order.
133 void sort_nodes_topologically();
134 void topological_sort_visit_node(Node *node, std::set<Node *> *visited_nodes, std::vector<Node *> *sorted_list);
136 // Used during finalize().
137 void propagate_gamma_and_color_space();
138 Node *find_output_node();
140 bool node_needs_colorspace_fix(Node *node);
141 void fix_internal_color_spaces();
142 void fix_output_color_space();
144 bool node_needs_gamma_fix(Node *node);
145 void fix_internal_gamma_by_asking_inputs(unsigned step);
146 void fix_internal_gamma_by_inserting_nodes(unsigned step);
147 void fix_output_gamma();
149 unsigned width, height;
150 ImageFormat output_format;
152 std::vector<Node *> nodes;
153 std::map<Effect *, Node *> node_map;
155 std::vector<Input *> inputs; // Also contained in nodes.
158 std::vector<Phase *> phases;
160 GLenum format, bytes_per_pixel;
164 #endif // !defined(_EFFECT_CHAIN_H)