-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include "blend.h"
-#include "camera.h"
-#include "effect.h"
-#include "except.h"
-#include "framebuffer.h"
-#include "lighting.h"
-#include "pipeline.h"
-#include "postprocessor.h"
-#include "renderbuffer.h"
-#include "tests.h"
-#include "texture2d.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-Pipeline::Pipeline(unsigned w, unsigned h, bool d):
- camera(0),
- width(w),
- height(h),
- hdr(d),
- fbo(0),
- color_buf(0),
- depth_buf(0)
-{ }
-
-Pipeline::~Pipeline()
-{
- delete fbo;
- delete color_buf;
- delete depth_buf;
-}
-
-void Pipeline::set_camera(const Camera *c)
-{
- camera = c;
-}
-
-PipelinePass &Pipeline::add_pass(const Tag &tag)
-{
- if(passes.count(tag))
- throw KeyError("Pass already exists");
-
- PipelinePass &pass = passes[tag];
- pass_order.push_back(tag);
- return pass;
-}
-
-PipelinePass &Pipeline::get_pass(const Tag &tag)
-{
- PassMap::iterator i = passes.find(tag);
- if(i==passes.end())
- throw KeyError("Unknown pass");
- return i->second;
-}
-
-const PipelinePass &Pipeline::get_pass(const Tag &tag) const
-{
- PassMap::const_iterator i = passes.find(tag);
- if(i==passes.end())
- throw KeyError("Unknown pass");
- return i->second;
-}
-
-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)
-{
- for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(i->renderable==&r)
- {
- renderables.erase(i);
- return;
- }
-}
-
-void Pipeline::add_effect(Effect &e)
-{
- effects.push_back(&e);
-}
-
-void Pipeline::add_postprocessor(PostProcessor &pp)
-{
- postproc.push_back(&pp);
- if(!fbo)
- {
- fbo = new Framebuffer;
-
- color_buf = new Texture2D;
- color_buf->set_min_filter(NEAREST);
- color_buf->set_mag_filter(NEAREST);
- color_buf->set_wrap(CLAMP_TO_EDGE);
- color_buf->storage((hdr ? RGB16F : RGB), width, height);
- fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
-
- depth_buf = new Texture2D;
- depth_buf->set_min_filter(NEAREST);
- depth_buf->set_mag_filter(NEAREST);
- depth_buf->set_wrap(CLAMP_TO_EDGE);
- depth_buf->storage(DEPTH_COMPONENT, width, height);
- fbo->attach(DEPTH_ATTACHMENT, *depth_buf, 0);
- }
-}
-
-void Pipeline::render(const Tag &tag) const
-{
- const PipelinePass &pass = get_pass(tag);
-
- Bind bind_depth_test(pass.depth_test);
- Bind bind_blend(pass.blend);
- Bind bind_lighting(pass.lighting);
-
- for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
- (*i)->prepare();
-
- for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(i->passes.empty() || i->passes.count(tag))
- i->renderable->render(tag);
-
- for(vector<Effect *>::const_iterator i=pass.effects.end(); i!=pass.effects.begin();)
- (*--i)->cleanup();
-}
-
-void Pipeline::render_all() const
-{
- if(camera)
- camera->apply();
-
- if(fbo)
- {
- fbo->bind();
- fbo->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
- }
-
- for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
- (*i)->prepare();
-
- for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
- render(*i);
-
- for(vector<Effect *>::const_iterator i=effects.end(); i!=effects.begin();)
- (*--i)->cleanup();
-
- if(fbo)
- Framebuffer::unbind();
-
- // XXX Need two color buffer textures to handle multiple post-processors correctly
- for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
- (*i)->render(*color_buf, *depth_buf);
-}
-
-
-Pipeline::Slot::Slot(const Renderable *r):
- renderable(r)
-{ }
-
-} // namespace GL
-} // namespace Msp