+++ /dev/null
-#include <msp/datafile/collection.h>
-#include <msp/io/print.h>
-#include <msp/strings/format.h>
-#include "error.h"
-#include "material.h"
-#include "renderpass.h"
-#include "program.h"
-#include "programdata.h"
-#include "renderer.h"
-#include "texture.h"
-#include "texture2d.h"
-#include "texturing.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-RenderPass::RenderPass():
- shprog(0),
- shprog_from_material(false),
- shdata(0),
- material(0),
- texturing(0),
- back_faces(false)
-{ }
-
-RenderPass::RenderPass(const RenderPass &other):
- shprog(other.shprog),
- shprog_from_material(other.shprog_from_material),
- shdata(other.shdata),
- uniform_slots(other.uniform_slots),
- material(other.material),
- material_slot(other.material_slot),
- texturing(other.texturing ? new Texturing(*other.texturing) : 0),
- tex_names(other.tex_names),
- back_faces(other.back_faces)
-{ }
-
-RenderPass &RenderPass::operator=(const RenderPass &other)
-{
- shprog = other.shprog;
- shprog_from_material = other.shprog_from_material;
- shdata = other.shdata;
- uniform_slots = other.uniform_slots;
- material = other.material;
- material_slot = other.material_slot;
- texturing = other.texturing ? new Texturing(*other.texturing) : 0;
- tex_names = other.tex_names;
- back_faces = other.back_faces;
- return *this;
-}
-
-RenderPass::~RenderPass()
-{
- delete texturing;
-}
-
-void RenderPass::finalize_material(DataFile::Collection *coll)
-{
- maybe_create_material_shader(coll);
- ensure_private_shader_data();
-
- if(!texturing)
- texturing = new Texturing;
- material->attach_textures_to(*texturing, *shdata);
-}
-
-void RenderPass::maybe_create_material_shader(DataFile::Collection *coll)
-{
- if(shprog && !shprog_from_material)
- return;
-
- if(coll)
- {
- shprog = material->create_compatible_shader(*coll);
- shprog.keep();
- }
- else
- throw invalid_operation("no collection");
-
- if(shdata)
- shdata = new ProgramData(*shdata, shprog.get());
-
- shprog_from_material = true;
-}
-
-void RenderPass::ensure_private_shader_data()
-{
- if(!shprog)
- throw invalid_operation("RenderPass::ensure_private_shader_data");
-
- if(!shdata)
- shdata = new ProgramData(shprog.get());
- else if(shdata.refcount()>1)
- shdata = new ProgramData(*shdata);
-}
-
-void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
-{
- shprog = prog;
- shprog.keep();
- shprog_from_material = false;
- shdata = (data ? new ProgramData(*data) : 0);
- if(material)
- finalize_material(0);
-}
-
-Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
-{
- map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
- if(i==uniform_slots.end())
- return Tag();
- return i->second;
-}
-
-void RenderPass::set_material(const Material *mat, DataFile::Collection *coll)
-{
- material = mat;
- material.keep();
- finalize_material(coll);
-}
-
-void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
-{
- if(!texturing)
- texturing = new Texturing;
-
- texturing->attach(index, *tex, (samp ? samp : texturing->get_attached_sampler(index)));
-}
-
-int RenderPass::get_texture_index(const string &n) const
-{
- map<string, unsigned>::const_iterator i = tex_names.find(n);
- if(i==tex_names.end())
- return -1;
- return i->second;
-}
-
-void RenderPass::apply(Renderer &renderer) const
-{
- renderer.set_texturing(texturing);
- renderer.set_material(material.get());
- renderer.set_shader_program(shprog.get(), shdata.get());
- renderer.set_reverse_winding(back_faces);
-}
-
-
-RenderPass::Loader::Loader(RenderPass &p):
- DataFile::CollectionObjectLoader<RenderPass>(p, 0)
-{
- init();
-}
-
-RenderPass::Loader::Loader(RenderPass &p, Collection &c):
- DataFile::CollectionObjectLoader<RenderPass>(p, &c)
-{
- init();
-}
-
-void RenderPass::Loader::init()
-{
- add("shader", &Loader::shader);
- add("material", &Loader::material_inline);
- add("material", &Loader::material);
- add("material_slot", &RenderPass::material_slot);
- add("back_faces",&RenderPass::back_faces);
- add("texunit", &Loader::texunit);
- add("texunit", &Loader::texunit_auto);
- add("texunit", &Loader::texunit_named);
- add("uniforms", &Loader::uniforms);
- add("uniform_slot", &Loader::uniform_slot);
- add("uniform_slot", &Loader::uniform_slot2);
-}
-
-// Temporary compatibility feature
-string RenderPass::Loader::get_shader_name(const string &n)
-{
- if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
- {
- IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
- return n+".shader";
- }
- return n;
-}
-
-void RenderPass::Loader::material_inline()
-{
- Material::GenericLoader ldr(coll);
- load_sub_with(ldr);
- obj.material = ldr.get_material();
- obj.finalize_material(coll);
-}
-
-void RenderPass::Loader::material(const string &name)
-{
- obj.material = &get_collection().get<Material>(name);
- obj.material.keep();
- obj.finalize_material(coll);
-}
-
-void RenderPass::Loader::shader(const string &n)
-{
- obj.shprog = &get_collection().get<Program>(get_shader_name(n));
- obj.shprog.keep();
- obj.shprog_from_material = false;
- if(obj.shdata)
- obj.shdata = new ProgramData(*obj.shdata, obj.shprog.get());
- if(obj.material)
- obj.finalize_material(coll);
-}
-
-void RenderPass::Loader::texunit(unsigned i)
-{
- if(!obj.texturing)
- obj.texturing = new Texturing;
- TextureLoader ldr(*obj.texturing, i, coll);
- load_sub_with(ldr);
-}
-
-void RenderPass::Loader::texunit_auto(const string &n)
-{
- if(!obj.texturing)
- obj.texturing = new Texturing;
- int i = obj.texturing->find_free_unit(n);
- if(i<0)
- throw runtime_error("no free texunit");
- texunit_named(i, n);
-}
-
-void RenderPass::Loader::texunit_named(unsigned i, const string &n)
-{
- texunit(i);
- obj.tex_names[n] = i;
- obj.ensure_private_shader_data();
- obj.shdata->uniform(n, static_cast<int>(i));
-}
-
-void RenderPass::Loader::uniforms()
-{
- obj.ensure_private_shader_data();
- load_sub(*obj.shdata);
-}
-
-void RenderPass::Loader::uniform_slot(const string &name)
-{
- uniform_slot2(name, name);
-}
-
-void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
-{
- obj.uniform_slots[slot] = name;
-}
-
-
-RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
- DataFile::CollectionObjectLoader<Texturing>(t, c),
- index(i),
- tex(0),
- samp(0)
-{
- add("sampler", &TextureLoader::sampler);
- add("texture", &TextureLoader::texture);
-}
-
-void RenderPass::TextureLoader::finish()
-{
- if(tex)
- obj.attach(index, *tex, samp);
- else if(samp)
- obj.attach(index, *samp);
-}
-
-void RenderPass::TextureLoader::sampler(const string &name)
-{
- samp = &get_collection().get<Sampler>(name);
-}
-
-void RenderPass::TextureLoader::texture(const string &name)
-{
- tex = &get_collection().get<Texture>(name);
-}
-
-} // namespace GL
-} // namespace Msp