#include "program.h"
#include "programdata.h"
#include "texture.h"
+#include "texture2d.h"
using namespace std;
RenderPass::RenderPass():
shprog(0),
shdata(0),
- own_material(false),
material(0)
{ }
RenderPass::RenderPass(const RenderPass &other):
shprog(other.shprog),
shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
- own_material(other.own_material),
- material(own_material ? new Material(*other.material) : other.material),
+ material(other.material),
textures(other.textures)
{ }
RenderPass::~RenderPass()
{
delete shdata;
- if(own_material)
- delete material;
}
void RenderPass::set_material(const Material *mat)
material = mat;
}
-unsigned RenderPass::get_texture_index(const string &slot) const
+void RenderPass::set_texture(unsigned index, const Texture *tex)
{
- for(unsigned i=0; i<textures.size(); ++i)
- if(textures[i].name==slot)
- return i;
-
- throw KeyError("Unknown texture slot", slot);
-}
+ for(vector<TextureSlot>::iterator i=textures.begin(); i!=textures.end(); ++i)
+ if(i->index==index)
+ {
+ i->texture = tex;
+ i->texture.keep();
+ return;
+ }
-void RenderPass::set_texture(const string &slot, const Texture *tex)
-{
- textures[get_texture_index(slot)] = tex;
+ throw KeyError("No texture slot for that unit", lexical_cast(index));
}
void RenderPass::bind() const
else if(old && old->material)
GL::Material::unbind();
- for(unsigned i=0; i<textures.size(); ++i)
- if(textures[i].texture)
- textures[i].texture->bind_to(i);
+ unsigned used_tex_units = 0;
+ for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
+ {
+ i->texture->bind_to(i->index);
+ used_tex_units |= 1<<i->index;
+ }
if(old)
{
- for(unsigned i=textures.size(); i<old->textures.size(); ++i)
- GL::Texture::unbind_from(i);
+ for(vector<TextureSlot>::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i)
+ if(!used_tex_units&(1<<i->index))
+ Texture::unbind_from(i->index);
}
}
}
+RenderPass::TextureSlot::TextureSlot(unsigned i):
+ index(i),
+ texture(0)
+{ }
+
+
RenderPass::Loader::Loader(RenderPass &p):
DataFile::CollectionObjectLoader<RenderPass>(p, 0)
{
allow_pointer_reload = false;
add("shader", &RenderPass::shprog);
- add("material", &Loader::material);
- add("material", &RenderPass::material);
- add("texture", &Loader::texture);
+ add("material", static_cast<void (Loader::*)()>(&Loader::material));
+ add("material", static_cast<void (Loader::*)(const string &)>(&Loader::material));
+ add("texunit", &Loader::texunit);
add("uniforms", &Loader::uniforms);
}
void RenderPass::Loader::finish()
{
- if(obj.shprog)
- {
- if(!obj.shdata)
- obj.shdata = new ProgramData;
-
- for(unsigned i=0; i<obj.textures.size(); ++i)
- {
- unsigned loc = obj.shprog->get_uniform_location(obj.textures[i].name);
- obj.shdata->uniform(loc, static_cast<int>(i));
- }
- }
+ // XXX Make shdata optional
+ if(obj.shprog && !obj.shdata)
+ obj.shdata = new ProgramData;
}
void RenderPass::Loader::material()
{
- if(obj.material)
- throw InvalidState("A material is already loaded");
-
RefPtr<Material> mat = new Material;
load_sub(*mat);
- obj.material = mat.release();
- obj.own_material = true;
+ obj.material = mat;
}
-void RenderPass::Loader::texture(const string &n)
+void RenderPass::Loader::material(const string &name)
{
- const Texture *tex = (n.empty() ? 0 : get_collection().get<Texture>(n));
- TextureSlot slot(tex);
- slot.name = (obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
+ obj.material = get_collection().get<Material>(name);
+ obj.material.keep();
+}
+
+void RenderPass::Loader::texunit(unsigned i)
+{
+ TextureSlot slot(i);
load_sub(slot);
obj.textures.push_back(slot);
}
}
-RenderPass::TextureSlot::TextureSlot(const Texture *t):
- texture(t)
-{ }
+RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
+ DataFile::CollectionObjectLoader<TextureSlot>(s, 0)
+{
+ init();
+}
+RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c):
+ DataFile::CollectionObjectLoader<TextureSlot>(s, &c)
+{
+ init();
+}
-RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
- DataFile::ObjectLoader<TextureSlot>(s)
+void RenderPass::TextureSlot::Loader::init()
+{
+ add("texture", &Loader::texture);
+ add("texture2d", &Loader::texture2d);
+}
+
+void RenderPass::TextureSlot::Loader::texture(const string &name)
+{
+ obj.texture = get_collection().get<Texture>(name);
+ obj.texture.keep();
+}
+
+void RenderPass::TextureSlot::Loader::texture2d()
{
- add("name", &TextureSlot::name);
+ RefPtr<Texture2D> tex = new Texture2D;
+ load_sub(*tex);
+ obj.texture = tex;
}
} // namespace GL
#ifndef MSP_GL_RENDERPASS_H_
#define MSP_GL_RENDERPASS_H_
+#include <msp/core/refptr.h>
#include <msp/datafile/objectloader.h>
#include "bindable.h"
void init();
virtual void finish();
void material();
- void shader(const std::string &);
- void texture(const std::string &);
+ void material(const std::string &);
+ void texunit(unsigned);
void uniforms();
};
private:
struct TextureSlot
{
- class Loader: public DataFile::ObjectLoader<TextureSlot>
+ class Loader: public DataFile::CollectionObjectLoader<TextureSlot>
{
public:
Loader(TextureSlot &);
+ Loader(TextureSlot &, Collection &);
+
+ private:
+ void init();
+ void texture(const std::string &);
+ void texture2d();
};
- const Texture *texture;
- std::string name;
+ unsigned index;
+ RefPtr<const Texture> texture;
- TextureSlot(const Texture *);
+ TextureSlot(unsigned);
};
Program *shprog;
ProgramData *shdata;
- bool own_material;
- const Material *material;
+ RefPtr<const Material> material;
std::vector<TextureSlot> textures;
RenderPass &operator=(const RenderPass &);
~RenderPass();
void set_material(const Material *);
- unsigned get_texture_index(const std::string &) const;
- void set_texture(const std::string &, const Texture *);
+ void set_texture(unsigned, const Texture *);
void bind() const;