]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.h
Improve pipeline render target management
[libs/gl.git] / source / pipeline.h
1 #ifndef MSP_GL_PIPELINE_H_
2 #define MSP_GL_PIPELINE_H_
3
4 #include <map>
5 #include <set>
6 #include "framebuffer.h"
7 #include "renderable.h"
8 #include "renderbuffer.h"
9 #include "texture2d.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class Blend;
15 class Camera;
16 class DepthTest;
17 class Lighting;
18 class PostProcessor;
19
20 /**
21 Encapsulates all of the information used to produce a complete image in the
22 framebuffer.  This is the highest level rendering class, combining Renderables
23 with a camera, lights and some other influential objects.
24
25 A Pipeline is also a Renderable itself.  Externally, it only exposes the
26 default pass.  Internally, it can hold any number of passes, which are invoked
27 in sequence when rendering the default pass is requested.  Each pass can have a
28 Lighting, a DepthTest and a Blend to control how it is rendered.
29
30 A Pipeline's render method should normally be called without a Renderer; it
31 will create one itself, using the camera specified for the Pipeline.  If a
32 Renderer is passed, its camera will be used instead.
33
34 Renderables are rendered in the order they were added to the Pipeline.  While
35 it's possible to remove renderables as well, using a Scene is recommended if
36 frequent add/remove operations are needed.
37
38 Pipelines may have post-processors to apply full-screen effects. Framebuffer
39 objects are automatically used to pass render results to the post-processors.
40 High dynamic range and multisample rendering can also be used.
41 */
42 class Pipeline: public Renderable
43 {
44 public:
45         class Pass
46         {
47         private:
48                 Tag tag;
49                 const Lighting *lighting;
50                 const DepthTest *depth_test;
51                 const Blend *blend;
52
53         public:
54                 Pass(const Tag &);
55
56                 const Tag &get_tag() const { return tag; }
57
58                 void set_lighting(const Lighting *);
59                 void set_depth_test(const DepthTest *);
60                 void set_blend(const Blend *);
61                 const Lighting *get_lighting() const { return lighting; }
62                 const DepthTest *get_depth_test() const { return depth_test; }
63                 const Blend *get_blend() const { return blend; }
64         };
65
66 private:
67         struct Slot
68         {
69                 const Renderable *renderable;
70                 std::set<Tag> passes;
71
72                 Slot(const Renderable *);
73         };
74
75         struct RenderTarget
76         {
77                 Framebuffer fbo;
78                 Texture2D color;
79                 Texture2D depth;
80
81                 RenderTarget(unsigned, unsigned, PixelFormat);
82         };
83
84         struct MultisampleTarget
85         {
86                 Framebuffer fbo;
87                 Renderbuffer color;
88                 Renderbuffer depth;
89
90                 MultisampleTarget(unsigned, unsigned, unsigned, PixelFormat);
91         };
92
93         typedef std::list<Pass> PassList;
94
95         PassList passes;
96         const Camera *camera;
97         std::vector<Slot> renderables;
98         std::vector<PostProcessor *> postproc;
99         unsigned width;
100         unsigned height;
101         bool hdr;
102         unsigned samples;
103         RenderTarget *target[2];
104         MultisampleTarget *target_ms;
105         mutable bool in_frame;
106
107 public:
108         Pipeline(unsigned, unsigned, bool = false);
109         ~Pipeline();
110
111         void set_hdr(bool);
112         void set_multisample(unsigned);
113         void set_camera(const Camera *);
114
115         Pass &add_pass(const Tag &tag);
116
117         void add_renderable(const Renderable &);
118         void add_renderable_for_pass(const Renderable &, const Tag &);
119         void remove_renderable(const Renderable &);
120         void add_postprocessor(PostProcessor &);
121
122         virtual void setup_frame() const;
123         virtual void finish_frame() const;
124
125         virtual void render(const Tag &tag = Tag()) const;
126         virtual void render(Renderer &, const Tag &tag = Tag()) const;
127
128 private:
129         void create_targets(unsigned);
130 };
131
132 } // namespace GL
133 } // namespace Msp
134
135 #endif