]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Make Tag directly comparable and use it as a key in relevant maps
[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))
41                 throw KeyError("Pass already exists");
42
43         PipelinePass &pass=passes[tag];
44         pass_order.push_back(tag);
45         return pass;
46 }
47
48 PipelinePass &Pipeline::get_pass(const Tag &tag)
49 {
50         PassMap::iterator i=passes.find(tag);
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         PassMap::const_iterator i=passes.find(tag);
59         if(i==passes.end())
60                 throw KeyError("Unknown pass");
61         return i->second;
62 }
63
64 void Pipeline::add_renderable(const Renderable &r)
65 {
66         renderables.push_back(&r);
67 }
68
69 void Pipeline::add_effect(Effect &e)
70 {
71         effects.push_back(&e);
72 }
73
74 void Pipeline::add_postprocessor(PostProcessor &pp)
75 {
76         postproc.push_back(&pp);
77         if(!fbo)
78         {
79                 fbo=new Framebuffer;
80                 color_buf=new Texture2D;
81                 color_buf->set_min_filter(NEAREST);
82                 color_buf->set_mag_filter(NEAREST);
83                 color_buf->storage((hdr ? RGB16F : RGB), width, height, 0);
84                 color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
85                 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
86                 depth_buf=new Renderbuffer;
87                 depth_buf->storage(DEPTH_COMPONENT, width, height);
88                 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
89                 Framebuffer::unbind();
90         }
91 }
92
93 void Pipeline::render(const Tag &tag) const
94 {
95         const PipelinePass &pass=get_pass(tag);
96         if(pass.lighting)
97                 pass.lighting->bind();
98         for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
99                 (*i)->prepare();
100         for(vector<const Renderable *>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
101                 (*i)->render(tag);
102         for(vector<Effect *>::const_iterator i=pass.effects.end(); i--!=pass.effects.begin();)
103                 (*i)->cleanup();
104         if(pass.lighting)
105                 Lighting::unbind();
106 }
107
108 void Pipeline::render_all() const
109 {
110         if(fbo)
111         {
112                 fbo->bind();
113                 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
114         }
115         for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
116                 (*i)->prepare();
117         for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
118                 render(*i);
119         for(vector<Effect *>::const_iterator i=effects.end(); i--!=effects.begin();)
120                 (*i)->cleanup();
121         if(fbo)
122                 Framebuffer::unbind();
123         // XXX Need two color buffer textures to handle multiple post-processors correctly
124         for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
125                 (*i)->render(*color_buf);
126 }
127
128 } // namespace GL
129 } // namespace Msp