#include "renderer.h"
#include "tests.h"
#include "texture2d.h"
+#include "view.h"
using namespace std;
namespace Msp {
namespace GL {
-Pipeline::Pipeline(unsigned w, unsigned h, bool d):
- camera(0),
- width(w),
- height(h),
- hdr(d),
- samples(0),
- target_ms(0),
- in_frame(false)
+Pipeline::Pipeline(unsigned w, unsigned h, bool d)
{
+ init(w, h);
+ hdr = d;
+}
+
+Pipeline::Pipeline(const View &view)
+{
+ init(view.get_width(), view.get_height());
+}
+
+Pipeline::Pipeline(const Framebuffer &fbo)
+{
+ init(fbo.get_width(), fbo.get_height());
+}
+
+void Pipeline::init(unsigned w, unsigned h)
+{
+ camera = 0;
+ width = w;
+ height = h;
+ hdr = false;
+ alpha = false;
+ samples = 0;
+ target_ms = 0;
target[0] = 0;
target[1] = 0;
}
if(h==hdr)
return;
- bool old_hdr= hdr;
+ bool old_hdr = hdr;
hdr = h;
try
{
}
}
+void Pipeline::set_alpha(bool a)
+{
+ if(a==alpha)
+ return;
+
+ bool old_alpha = alpha;
+ alpha = a;
+ try
+ {
+ create_targets(2);
+ }
+ catch(...)
+ {
+ alpha = old_alpha;
+ throw;
+ }
+}
+
void Pipeline::set_multisample(unsigned s)
{
if(s==samples)
}
}
-void Pipeline::set_camera(const Camera *c)
+Pipeline::Pass &Pipeline::add_pass(const Tag &tag, Renderable &r)
{
- camera = c;
-}
-
-Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
-{
- passes.push_back(Pass(tag, 0));
+ passes.push_back(Pass(tag, &r));
return passes.back();
}
-void Pipeline::add_renderable(const Renderable &r)
-{
- for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(i->renderable==&r)
- {
- i->passes.clear();
- return;
- }
-
- renderables.push_back(&r);
-}
-
-void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
-{
- for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(i->renderable==&r)
- {
- i->passes.insert(tag);
- return;
- }
-
- renderables.push_back(&r);
- renderables.back().passes.insert(tag);
-}
-
-void Pipeline::remove_renderable(const Renderable &r)
+void Pipeline::add_postprocessor(PostProcessor &pp)
{
- for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(i->renderable==&r)
- {
- renderables.erase(i);
- return;
- }
+ add_postprocessor(&pp, true);
}
-Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r)
+void Pipeline::add_postprocessor_owned(PostProcessor *pp)
{
- passes.push_back(Pass(tag, &r));
- return passes.back();
+ add_postprocessor(pp, false);
}
-void Pipeline::add_postprocessor(PostProcessor &pp)
+void Pipeline::add_postprocessor(PostProcessor *pp, bool keep)
{
- postproc.push_back(&pp);
+ postproc.push_back(pp);
+ if(keep)
+ postproc.back().keep();
try
{
create_targets(0);
}
}
-void Pipeline::setup_frame() const
+void Pipeline::setup_frame(Renderer &renderer)
{
- in_frame = true;
for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
- if(const Renderable *renderable = i->get_renderable())
- renderable->setup_frame();
+ if(Renderable *renderable = i->get_renderable())
+ renderable->setup_frame(renderer);
for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- i->renderable->setup_frame();
+ i->renderable->setup_frame(renderer);
}
-void Pipeline::finish_frame() const
+void Pipeline::finish_frame()
{
- in_frame = false;
for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
- if(const Renderable *renderable = i->get_renderable())
+ if(Renderable *renderable = i->get_renderable())
renderable->finish_frame();
for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
i->renderable->finish_frame();
}
-void Pipeline::render(const Tag &tag) const
-{
- if(tag.id)
- return;
-
- Renderer renderer(camera);
- render(renderer, tag);
-}
-
void Pipeline::render(Renderer &renderer, const Tag &tag) const
{
if(tag.id)
return;
- bool was_in_frame = in_frame;
- if(!in_frame)
- setup_frame();
-
const Framebuffer *out_fbo = Framebuffer::current();
- // XXX Make sure the correct FBO is restored if an exception is thrown
+ // These are no-ops but will ensure the related state gets restored
+ BindRestore restore_fbo(out_fbo);
+ BindRestore restore_depth_test(DepthTest::current());
+ BindRestore restore_blend(Blend::current());
if(target[0])
{
- Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
+ Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
fbo.bind();
- fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
+ fbo.clear();
}
for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
{
- Bind bind_depth_test(i->get_depth_test());
- Bind bind_blend(i->get_blend());
+ if(const DepthTest *dt = i->get_depth_test())
+ dt->bind();
+ else
+ DepthTest::unbind();
+
+ if(const Blend *b = i->get_blend())
+ b->bind();
+ else
+ Blend::unbind();
+
renderer.set_lighting(i->get_lighting());
renderer.set_clipping(i->get_clipping());
renderer.render(*j->renderable, i->get_tag());
}
- renderer.end();
-
if(target[0])
{
+ DepthTest::unbind();
+ Blend::unbind();
+
if(samples)
- target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
+ target[0]->blit_from(*target_ms);
for(unsigned i=0; i<postproc.size(); ++i)
{
unsigned j = i%2;
if(i+1<postproc.size())
- target[1-j]->fbo.bind();
+ target[1-j]->get_framebuffer().bind();
else
out_fbo->bind();
- postproc[i]->render(target[j]->color, target[j]->depth);
+ const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
+ const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
+ postproc[i]->render(renderer, color, depth);
}
-
- out_fbo->bind();
}
-
- if(!was_in_frame)
- finish_frame();
}
void Pipeline::create_targets(unsigned recreate)
target_ms = 0;
}
- PixelFormat fmt = (hdr ? RGB16F : RGB);
+ PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
+ RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
if(!postproc.empty() || samples)
{
if(!target[0])
}
if(!target_ms && samples)
- target_ms = new MultisampleTarget(width, height, samples, fmt);
+ target_ms = new RenderTarget(width, height, samples, fmt);
}
-Pipeline::Pass::Pass(const Tag &t, const Renderable *r):
+Pipeline::Pass::Pass(const Tag &t, Renderable *r):
tag(t),
lighting(0),
depth_test(0),
}
-Pipeline::Slot::Slot(const Renderable *r):
+Pipeline::Slot::Slot(Renderable *r):
renderable(r)
{ }
-
-Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
-{
- color.set_min_filter(NEAREST);
- color.set_mag_filter(NEAREST);
- color.set_wrap(CLAMP_TO_EDGE);
- color.storage(f, w, h);
- fbo.attach(COLOR_ATTACHMENT0, color, 0);
-
- depth.set_min_filter(NEAREST);
- depth.set_mag_filter(NEAREST);
- depth.set_wrap(CLAMP_TO_EDGE);
- depth.storage(DEPTH_COMPONENT, w, h);
- fbo.attach(DEPTH_ATTACHMENT, depth, 0);
-
- fbo.require_complete();
-}
-
-
-Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
-{
- color.storage_multisample(s, f, w, h);
- fbo.attach(COLOR_ATTACHMENT0, color);
-
- depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
- fbo.attach(DEPTH_ATTACHMENT, depth);
-
- fbo.require_complete();
-}
-
} // namespace GL
} // namespace Msp