]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Add a Pipeline framework for constructing complex rendering sequences
[libs/gl.git] / source / pipeline.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "effect.h"
9 #include "except.h"
10 #include "framebuffer.h"
11 #include "lighting.h"
12 #include "pipeline.h"
13 #include "postprocessor.h"
14 #include "renderbuffer.h"
15 #include "texture2d.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GL {
21
22 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
23         width(w),
24         height(h),
25         hdr(d),
26         fbo(0),
27         color_buf(0),
28         depth_buf(0)
29 { }
30
31 Pipeline::~Pipeline()
32 {
33         delete fbo;
34         delete color_buf;
35         delete depth_buf;
36 }
37
38 PipelinePass &Pipeline::add_pass(const Tag &tag)
39 {
40         if(passes.count(tag.id))
41                 throw KeyError("Pass already exists");
42
43         PipelinePass &pass=passes[tag.id];
44         pass_order.push_back(tag);
45         return pass;
46 }
47
48 PipelinePass &Pipeline::get_pass(const Tag &tag)
49 {
50         map<unsigned, PipelinePass>::iterator i=passes.find(tag.id);
51         if(i==passes.end())
52                 throw KeyError("Unknown pass");
53         return i->second;
54 }
55
56 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
57 {
58         map<unsigned, PipelinePass>::const_iterator i=passes.find(tag.id);
59         if(i==passes.end())
60                 throw KeyError("Unknown pass");
61         return i->second;
62 }
63
64 bool Pipeline::has_pass(const Tag &tag) const
65 {
66         return passes.count(tag.id);
67 }
68
69 void Pipeline::add_renderable(const Renderable &r)
70 {
71         renderables.push_back(&r);
72 }
73
74 void Pipeline::add_effect(Effect &e)
75 {
76         effects.push_back(&e);
77 }
78
79 void Pipeline::add_postprocessor(PostProcessor &pp)
80 {
81         postproc.push_back(&pp);
82         if(!fbo)
83         {
84                 fbo=new Framebuffer;
85                 color_buf=new Texture2D;
86                 color_buf->set_min_filter(NEAREST);
87                 color_buf->set_mag_filter(NEAREST);
88                 color_buf->storage((hdr ? RGB16F : RGB), width, height, 0);
89                 color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
90                 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
91                 depth_buf=new Renderbuffer;
92                 depth_buf->storage(DEPTH_COMPONENT, width, height);
93                 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
94                 Framebuffer::unbind();
95         }
96 }
97
98 void Pipeline::render(const Tag &tag) const
99 {
100         const PipelinePass &pass=get_pass(tag);
101         if(pass.lighting)
102                 pass.lighting->bind();
103         for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
104                 (*i)->prepare();
105         for(vector<const Renderable *>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
106                 (*i)->render(tag);
107         for(vector<Effect *>::const_iterator i=pass.effects.end(); i--!=pass.effects.begin();)
108                 (*i)->cleanup();
109         if(pass.lighting)
110                 Lighting::unbind();
111 }
112
113 void Pipeline::render_all() const
114 {
115         if(fbo)
116         {
117                 fbo->bind();
118                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
119         }
120         for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
121                 (*i)->prepare();
122         for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
123                 render(*i);
124         for(vector<Effect *>::const_iterator i=effects.end(); i--!=effects.begin();)
125                 (*i)->cleanup();
126         if(fbo)
127                 Framebuffer::unbind();
128         // XXX Need two color buffer textures to handle multiple post-processors correctly
129         for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
130                 (*i)->render(*color_buf);
131 }
132
133 } // namespace GL
134 } // namespace Msp