#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)
-{
- camera = c;
-}
-
-Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
+Pipeline::Pass &Pipeline::add_pass(const Tag &tag, Renderable &r)
{
- 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)
+void Pipeline::add_postprocessor(PostProcessor &pp)
{
- 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);
+ add_postprocessor(&pp, true);
}
-void Pipeline::remove_renderable(const Renderable &r)
+void Pipeline::add_postprocessor_owned(PostProcessor *pp)
{
- for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(i->renderable==&r)
- {
- renderables.erase(i);
- return;
- }
+ add_postprocessor(pp, false);
}
-Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r)
+void Pipeline::add_postprocessor(PostProcessor *pp, bool keep)
{
- passes.push_back(Pass(tag, &r));
- return passes.back();
-}
-
-void Pipeline::add_postprocessor(PostProcessor &pp)
-{
- 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();
- // These is a no-ops but will ensure the related state gets restored
+ // 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());
{
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)
{
if(const DepthTest *dt = i->get_depth_test())
postproc[i]->render(renderer, color, depth);
}
}
-
- if(!was_in_frame)
- finish_frame();
}
void Pipeline::create_targets(unsigned recreate)
target_ms = 0;
}
- PixelFormat color_pf = (hdr ? RGB16F : RGB);
+ PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA : RGB));
RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
if(!postproc.empty() || samples)
{
}
-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)
{ }