]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Notify Renderables about the start and end of a frame
[libs/gl.git] / source / pipeline.cpp
1 #include <msp/core/maputils.h>
2 #include "blend.h"
3 #include "camera.h"
4 #include "framebuffer.h"
5 #include "lighting.h"
6 #include "pipeline.h"
7 #include "postprocessor.h"
8 #include "renderbuffer.h"
9 #include "renderer.h"
10 #include "tests.h"
11 #include "texture2d.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
19         camera(0),
20         width(w),
21         height(h),
22         hdr(d),
23         samples(0),
24         target_ms(0),
25         in_frame(false)
26 {
27         target[0] = 0;
28         target[1] = 0;
29 }
30
31 Pipeline::~Pipeline()
32 {
33         delete target[0];
34         delete target[1];
35         delete target_ms;
36 }
37
38 void Pipeline::set_hdr(bool h)
39 {
40         hdr = h;
41         create_targets(true);
42 }
43
44 void Pipeline::set_multisample(unsigned s)
45 {
46         samples = s;
47         create_targets(false);
48 }
49
50 void Pipeline::set_camera(const Camera *c)
51 {
52         camera = c;
53 }
54
55 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
56 {
57         passes.push_back(Pass(tag));
58         return passes.back();
59 }
60
61 void Pipeline::add_renderable(const Renderable &r)
62 {
63         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
64                 if(i->renderable==&r)
65                 {
66                         i->passes.clear();
67                         return;
68                 }
69
70         renderables.push_back(&r);
71 }
72
73 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
74 {
75         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
76                 if(i->renderable==&r)
77                 {
78                         i->passes.insert(tag);
79                         return;
80                 }
81
82         renderables.push_back(&r);
83         renderables.back().passes.insert(tag);
84 }
85
86 void Pipeline::remove_renderable(const Renderable &r)
87 {
88         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
89                 if(i->renderable==&r)
90                 {
91                         renderables.erase(i);
92                         return;
93                 }
94 }
95
96 void Pipeline::add_postprocessor(PostProcessor &pp)
97 {
98         postproc.push_back(&pp);
99         create_targets(false);
100 }
101
102 void Pipeline::setup_frame() const
103 {
104         in_frame = true;
105         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
106                 i->renderable->setup_frame();
107 }
108
109 void Pipeline::finish_frame() const
110 {
111         in_frame = false;
112         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
113                 i->renderable->finish_frame();
114 }
115
116 void Pipeline::render(const Tag &tag) const
117 {
118         if(tag.id)
119                 return;
120
121         Renderer renderer(camera);
122         render(renderer, tag);
123 }
124
125 void Pipeline::render(Renderer &renderer, const Tag &tag) const
126 {
127         if(tag.id)
128                 return;
129
130         bool was_in_frame = in_frame;
131         if(!in_frame)
132                 setup_frame();
133
134         if(target[0])
135         {
136                 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
137                 // XXX exception safety
138                 fbo.bind();
139                 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
140         }
141
142         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
143         {
144                 Bind bind_depth_test(i->get_depth_test());
145                 Bind bind_blend(i->get_blend());
146                 Bind bind_lighting(i->get_lighting());
147
148                 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
149                         if(j->passes.empty() || j->passes.count(i->get_tag()))
150                                 renderer.render(*j->renderable, i->get_tag());
151         }
152
153         if(target[0])
154         {
155                 if(samples)
156                         target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
157
158                 for(unsigned i=0; i<postproc.size(); ++i)
159                 {
160                         unsigned j = i%2;
161                         if(i+1<postproc.size())
162                                 target[1-j]->fbo.bind();
163                         else
164                                 Framebuffer::unbind();
165                         postproc[i]->render(target[j]->color, target[j]->depth);
166                 }
167
168                 Framebuffer::unbind();
169         }
170
171         if(!was_in_frame)
172                 finish_frame();
173 }
174
175 void Pipeline::create_targets(bool recreate)
176 {
177         if(recreate)
178         {
179                 delete target[0];
180                 delete target[1];
181                 delete target_ms;
182                 target[0] = 0;
183                 target[1] = 0;
184                 target_ms = 0;
185         }
186
187         PixelFormat fmt = (hdr ? RGB16F : RGB);
188         if(!target[0])
189                 target[0] = new RenderTarget(width, height, fmt);
190         if(!target[1] && postproc.size()>1)
191                 target[1] = new RenderTarget(width, height, fmt);
192
193         if(!target_ms && samples)
194                 target_ms = new MultisampleTarget(width, height, samples, fmt);
195 }
196
197
198 Pipeline::Pass::Pass(const Tag &t):
199         tag(t),
200         lighting(0),
201         depth_test(0),
202         blend(0)
203 { }
204
205 void Pipeline::Pass::set_lighting(const Lighting *l)
206 {
207         lighting = l;
208 }
209
210 void Pipeline::Pass::set_depth_test(const DepthTest *d)
211 {
212         depth_test = d;
213 }
214
215 void Pipeline::Pass::set_blend(const Blend *b)
216 {
217         blend = b;
218 }
219
220
221 Pipeline::Slot::Slot(const Renderable *r):
222         renderable(r)
223 { }
224
225
226 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
227 {
228         color.set_min_filter(NEAREST);
229         color.set_mag_filter(NEAREST);
230         color.set_wrap(CLAMP_TO_EDGE);
231         color.storage(f, w, h);
232         fbo.attach(COLOR_ATTACHMENT0, color, 0);
233
234         depth.set_min_filter(NEAREST);
235         depth.set_mag_filter(NEAREST);
236         depth.set_wrap(CLAMP_TO_EDGE);
237         depth.storage(DEPTH_COMPONENT, w, h);
238         fbo.attach(DEPTH_ATTACHMENT, depth, 0);
239 }
240
241
242 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
243 {
244         color.storage_multisample(s, f, w, h);
245         fbo.attach(COLOR_ATTACHMENT0, color);
246
247         depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
248         fbo.attach(DEPTH_ATTACHMENT, depth);
249 }
250
251 } // namespace GL
252 } // namespace Msp